Open link in new tab works in Firefox but not in Chrome with Selenium


Skuma

I have a test where I need to open a link in a new tab. This must work fine in Firefox and Chrome. I tried it first using the Gmail link on the Google page.

On Firefox it worked perfectly, opened Gmail in a new tab. But on Chrome, the Gmail page opens in the same window and the menu remains open after right-clicking. Has anyone encountered this problem?

Below is my sample code.

Firefox code:

FirefoxProfile myprofile;
ProfilesIni profile = new ProfilesIni();            
myprofile = profile.getProfile("SeleniumAuto");             
WebDriver driver = new FirefoxDriver(myprofile);
driver.get("http://www.google.com");    
driver.manage().window().maximize();

Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
 .sendKeys(Keys.ENTER).build().perform();            

Chrome code:

ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");    
driver.manage().window().maximize();*/    

Actions a = new Actions(driver);
WebElement e = driver.findElement(By.xpath("/html/body/div/div[3]/div[1]/div/div/div/div[1]/div[2]/a"));
a.moveToElement(e);
a.contextClick(e).sendKeys(Keys.ARROW_DOWN)
 .sendKeys(Keys.ENTER).build().perform();
fashionable

I had the same problem. Apparently ARROW_DOWN didn't work, so I tried using the key combination and it worked for me. code show as below:

1) Open in new tab with focus still on current tab

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.CONTROL);

2) Open in new tab and move to new tab

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(your_path)));
actions.keyDown(Keys.CONTROL).perform();
actions.keyDown(Keys.SHIFT).perform();
driver.findElement(By.xpath(your_path)).click();
actions.keyUp(Keys.SHIFT);
actions.keyUp(Keys.CONTROL);

Hope this helps.

Related


Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and you need to verify the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and con

Selenium - click on the link to open a new tab

tree 55 I've seen a lot of threads on how to open a link in a new tab, but what if you have a link that creates a new tab and need to validate the title? All I need to do is click the link -> confirm the new tab has the correct title -> close the tab and conti

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

Selenium 2: open link in new tab and close tab

Alps: I want to be able to open links in new tabs in Selenium 2. I also want to close the tab after I finish interacting with the page. How is this possible if I have WebElementa <a>tag? I'm using Selenium 2's Java API with the Firefox driver running on Firefo

Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done, my links stop responding to left clicks. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the browse

Correct javascript redirect works with "open link in new tab"

Marco Prince Is there a way to create links using JavaScript exactly like <a>markup ? In other words, when you click it normally, it opens in the current window, and when you right click, it says "Open link in new tab" under options. If you click on it, it doe

Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done my links stop responding to left clicks too. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the bro

Left click on link doesn't work, but "open in new tab" works

rock degree I'm working on my personal website (built from a template) and when I'm done my links stop responding to left clicks too. They work fine if you right click and select "Open in New Tab". You can see the URL in the status bar at the bottom of the bro

Open link in new tab

no mice here I need to open a link <a>in a new tab/window 's tab, do something there and then close it. html code: <body> <a id="uniqueid" href="somelink">I need to open this in a new tab</a> </body> python code: from selenium import webdriver driver = w

Perl - WWW::Mechanize::Firefox - open link in new tab

Dr. Avalanche Using that $mech->find_all_links_dommethod, I get a set of links on the page. For each array $linkin the array I want to open it in a new tab. I don't know what to do, advice would be great. luciferin kills Here's one way of working: #!/usr/bin/p

Open new tab on selection change - only works in Chrome

Jowan I have a regular selectelement: <div id="standings-listing"> <select> <option value="0">please select</option> <option value="1">one</option> <option value="2">two</option> </select> </div> Despite my attempts to convince

Open new tab on selection change - only works in Chrome

Jowan I have a regular selectelement: <div id="standings-listing"> <select> <option value="0">please select</option> <option value="1">one</option> <option value="2">two</option> </select> </div> Despite my attempts to convince

Ctrl + t - Can't open new tab in Selenium + Firefox Python

Wren Has Selenium removed any functionality? Goal: Open a new tab like "Ctrl+t" environment: Windows 7 Firefox 68 Python 3.7 The following two scripts are unresponsive: # 1 dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') # 2 ActionChains(dr

Ctrl + t - Can't open new tab in Selenium + Firefox Python

Wren Has Selenium removed any functionality? Goal: Open a new tab like "Ctrl+t" environment: Windows 7 Firefox 68 Python 3.7 The following two scripts are unresponsive: # 1 dr.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't') # 2 ActionChains(dr

How to open a link in a new tab in a menu using Selenium in Java

Ashish Savaliya I am trying to open a link in a new Tab in Selenium Java, but the first time only one link opens, but when opening the second link the For Loop is giving an error, can anyone help me with this. Here is my code. public class Link_Open_In_New_Tab

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none

How to open link in new tab using python and selenium

User 3820991 I want to open links found on a website in a new tab. I tried to open a new tab and passed the link URL to the motorists suggested here however, the new tab does not open at all. (There are some other suggestions on how to open new tabs, but none

How to open a link in a new tab in a menu using Selenium in Java

Ashish Savaliya I am trying to open a link in a new Tab in Selenium Java, but the first time only one link opens, but when opening the second link the For Loop is giving an error, can anyone help me with this. Here is my code. public class Link_Open_In_New_Tab