Selecting right click option from context menu in selenium - selenium

I have a scenario where i need to right click on a link, when context menu appears i need to click on option "Open link in incognito window".
When i try to achieve this by below code snippet i could see a context menu appears but instead of launching the link in new window the link gets launched in same window.
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("http://www.google.com/");
Actions action = new Actions(driver);
WebElement ele = driver.findElement(By.linkText("About"));
action.contextClick(ele).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
any suggestions how to make this happen?

There may be a keyboard shortcut for opening a link in an incognito window but I was unable to find one but I didn't look for long. You may have better luck.
Another method would be to grab the URL of the link you want to click, open an incognito window using the keyboard shortcut (CTRL+SHIFT+N), and then navigating to the URL. It's not ideal but I don't know of another way to do this.
Is there some reason you can't just start your script in an incognito window and proceed from there?

This context menu is not a part of html page, but this is a menu coming from Chrome (a desktop application)
Selenium/WebDriver does not have the capability to automate the desktop applications, you can use AutoIt or similar software to do it
But all what you need is just to open a new browser session (incognito window is nothing but a new, independent browser session).
To simulate this behaviour using WebDriver just open a new Chrome driver:
WebDriver drv1 = new ChromeDriver();
drv1.get("https://www.google.pl");
drv1.findElement(By.name("q")).sendKeys("About");
........
........
// This will open a new browser window with a new, independent browser session
WebDriver drv2 = new ChromeDriver();
drv2.get("https://www.google.pl");
drv2.findElement(By.name("q")).sendKeys("Hello");
........
........
drv1.do-Something-in-session-1
.....
drv2.do-Something-other-in-session-2
....

Related

Windows vs tabs in Selenium

As per Difference between webdriver.Dispose(), .Close() and .Quit()
driver.close closes the current active window where driver.quit closes all of the windows. What I get is if I opened a new tab in my current window and then called driver.close the whole windows including my tabs would close. But when I run that code
driver.get("http://testingpool.com/selenium-ide-part-1/");
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
only the first tabs gets closed. Also I find that my allWindowHandles has length of 2 although I have only one window.
I was trying to open a new window using Selenium as per https://www.tutorialspoint.com/how-to-open-a-new-window-on-a-browser-using-selenium-webdriver-for-python using
((JavascriptExecutor)driver).executeScript("window.open('')");
but that resulted in a new tab not a new window.
I am confused if Selenium differentiates between a tab and a window at all or not.
With the terminology used to distinguish between driver.close() and driver.quit() methods supported by Selenium WebDriver in the post you are referencing to, they actually mostly mean browser tabs, not windows.
By opening a new browser window with
((JavascriptExecutor)driver).executeScript("window.open('')");
or by clicking on some web element opening a new browser tab/window is will normally open a new tab in the existing browser.
What I get is if I opened a new tab in my current window and then
called driver.close the whole window including my tabs would close.
But when I run that code
See you may have opened a new tab by clicking on link or using JavascriptExecutor but Selenium still has focus on first tab of the first window.
Therefore, when you do this:
ArrayList<String> allWindowHandles = new ArrayList<> (driver.getWindowHandles());
driver.close()
It should close the first tab or window, since you have not switched to new tab/window.
Also I find that my allWindowHandles has length of 2
this is cause you have opened a new tab using this line
driver.findElement(By.xpath("//*[#id='content']/div[2]/article/div/div[2]/div[1]/p[8]/span/a")).click(); //opens a new tab
the moment you open a new tab by clicking on a link or Using JavascriptExecutor, you will have +1 windows handle.
However, if you do not switch to any tab or window/s. Selenium will always have focus on first tab of the first window.
Also, window.open('') is Js command not Selenium.
there is a difference between a new tab or windows. But if you take Selenium into consideration, We switch to new tab or windows in a same way.
However to open a new window, you would have to simulate CTRL+N keyboard action.

IWebDriver opens a blank data window after clicking on button instead of the loaded content that suppose to be - selenium c#

I have selenium.support version 3.141.0, chrome driver version 2.43.0 and Google Chrome version 71.0.3578.98 and Selenium.ChromeDriver.dll 2.43.0.
when i click on a certain button a new window should be open. i click on the button and if i use any action on the browser for example new WebDriverWait(_driver, TimeSpan.FromSeconds(60)).Until(IsPageLoaded); the window stop to load and stay blank data. why can't i get the browser to load it's content?
Edit: when the new window opens i change the the driver to the latest windowHandel and use the webDriverWait from above to wait which result in a blank data window, but if i use thread.sleep after changing the windowHandel the window load its content
You can try with a explicit wait for a element to be visible on the new window. That would ensure it waits for the contents to load up

Can't switch from Full screen mode to Maximize mode when automating IE using Selenium webdriver

Someone help me.
This is my code.
System.setProperty("webdriver.ie.driver", "D:\\Personal\\others\\New folder\\Selenium\\Software\\Software\\drivers\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver();
driver.get("http://facebook.com");
driver.manage().window().fullscreen();
driver.manage().window().maximize();
I used Fullscreen method followed by maximize method. Once the screen went into fullscreen it did not switch back to maximize mode even, if I manually pressed F11 after the automation script completed. This scenario works in other browsers.
There's no need of going to full screen if you want your browser to get maximize.
driver.manage().window().maximize();
This code will let you maximize your browser during the run.
If you want to resize your window to a specific size in that case you can use the following:
Dimension d = new Dimension(420,600);
driver.manage().window().setSize(d);
This will let you resize your browser as per the height and width that you are providing.
Hope this helps!!

Webdriver doesn't open an upload window

I try to click on an element, which opens an windows model (choose a file..).
When I try to click on this button, no upload button is opened (But when I click on it manually it opens. Also, I see WebDriver success to find the element - the button is marked, but no window is opened.)
I try the next, none of them works:
1. currentPopup.click();
2. new Actions(driver).click(currentPopup).perform();
3. JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", currentPopup);
Do not click that button. Selenium does not popup the windows open file.
The work around is
driver.findElement(By.id("UploadElementID")).sendKeys("<absolutePathToFile>");
UploadElementID is the id of that element (input type="file") and in sendKeys you have to specify the absolute path of the content you want to upload (Image,video etc). Selenium will do the rest for you

Selenium WebDriver: Open new tab instead of a new window

I am using Selenium WebDriver. Every link is opened in a new browser window. It is not convenient for me. How can I change it so that it opens just in new tab?
Selenium has the ability to switch over tabs now-a-days. The below code1: will work for firefox, code2: for IE and chrome by using Robot class we can do and the control doesnt move automatically to current tab so we need to switch to the current tab by using window handles method. The given below code will work well When we are running individual script but when running as a suite you may feel the pain in performing key board events. In order to avoid that we got to go with other possibility by using user defined javascript method by using javascript executor in selenium-Java.
We can switch between windows and tabs by identifying its name allocated for each and every windows which we open and the name will be in alphanumeric character.
***Code 1***
//First tab(default tab)
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
//second tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.navigate().to("https://yahoo.com");
//third tab
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "t");
driver.navigate().to("http://www.google.com");
//move to very first tab.
driver.findElement(By.cssSelector("body"))
.sendKeys(Keys.CONTROL + "\t");
// To close the current tab.
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "w");
**code 2**
driver.navigate().to("http://www.google.com");
driver.manage().window().maximize();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_T);
Set<String> handles = driver.getWindowHandles();
List<String> handlesList = new ArrayList<String>(handles);
String newTab = handlesList.get(handlesList.size() - 1);
// switch to new tab
driver.switchTo().window(newTab);
driver.get("http://www.yahoo.com");
Selenium has no ability to switch tabs at the moment. Because of this we force the browser to open links in new windows but since we are able to switch windows we force the browser to take the approach. This may be fixed in a later version
If you don't care about switching between tabs, which is a high probability, just use the following options in firefox:
FirefoxOptions options = new FirefoxOptions().addPreference("browser.link.open_newwindow", 3);
WebDriver driver1 = new new FirefoxDriver(options);
As the first link below explains, Selenium wanted to give the ability to users to switch between tabs. The workaround to achieve this was to instruct the browser to open tabs as windows. This however has some undesirable side-efects like:
you have to manually switch to the tab that a link just opened (and use tricks to find which is this tab).
you have to manually switch back to the original tab if you close the current one.
The code above reverts this option for firefox.
Links:
https://github.com/SeleniumHQ/selenium/issues/6865#issuecomment-456698824
https://www.selenium.dev/documentation/webdriver/interactions/windows/#switching-windows-or-tabs
http://kb.mozillazine.org/Browser.link.open_newwindow