Opening new tabs in Firefox and Chrome with Selenium doesn't work


Barrio (j.barrio):

I read a lot of options related to the way new windows are opened with Selenium. All the questions and answers are from years ago, maybe that's why they didn't work for me. That's why I want to ask this question again.

My first approach is to use javascript action:

((JavascriptExecutor) getDriver()).executeScript("window.open('','NewWindow');");

My problem is different results in Firefox and Chrome. Firefox opens a new window, Chrome opens a new tab. This means my test cases won't work if I execute in a different browser.

After that, I thought about another approach. If I send the shortcut to open a new tab, probably both browsers have the same behavior. This started my nightmare. In current Chrome and Firefox versions, none of the following options will open anything:

  1. Send key connection shortcut:
    getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.COMMAND+"T");
    
  2. Send keystrokes Multiple keystroke sequences:
    getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.COMMAND,"T");
    
  3. send chord
    getDriver().findElement(By.xpath(".//body")).sendKeys(Keys.chord(Keys.COMMAND + "T"));
    
  4. use action
    final Actions builder = new Actions(getDriver());
    builder.keyDown(Keys.COMMAND).sendKeys("T").perform();
    

I'm thinking of trying to use the COMMAND key to click down on any link, but maybe there's another easy way to open a new tab in a different browser. Here is my question, are you now an efficient way to open a new tab instead of a new window in a different browser for the same operation?

extra information

Selenium Version -> 3.141.59

Chrome version -> 79.0.3945.79

Firefox version -> 70.0.1

Thank you in advance.

Barrio (j.barrio):

When a user asked me how I ended up solving this problem, my solution was:

// Save the current window reference
final String parentWindow = driver.getWindowHandle();
// Look for the element I would like to click
final WebElement elem = driver.findElement(By.xpath(xpath));
// Create an action to be performed
final Actions builder = new Actions(driver);
// The OSKEY is a global variable where depending on the OS is saved CMD or CTR
// With the special key chord, the program clicks on the element
builder.keyDown(OSKEY).click(elem).perform();

For me, there is no browser issue.

Related


Opening new tabs in Firefox and Chrome with Selenium doesn't work

Barrio (j.barrio): I read a lot of options related to the way new windows are opened with Selenium. All the questions and answers are from years ago, maybe that's why they didn't work for me. That's why I want to ask this question again. My first approach is t

Opening new tabs in Firefox and Chrome with Selenium doesn't work

Barrio (j.barrio): I read a lot of options related to the way new windows are opened with Selenium. All the questions and answers are from years ago, maybe that's why they didn't work for me. That's why I want to ask this question again. My first approach is t

Opening new tabs in Firefox and Chrome with Selenium doesn't work

Barrio (j.barrio): I read a lot of options related to the way new windows are opened with Selenium. All the questions and answers are from years ago, maybe that's why they didn't work for me. That's why I want to ask this question again. My first approach is t

Opening new tabs in Firefox and Chrome with Selenium doesn't work

Barrio (j.barrio): I read a lot of options related to the way new windows are opened with Selenium. All the questions and answers are from years ago, maybe that's why they didn't work for me. That's why I want to ask this question again. My first approach is t

Selenium opening multiple tabs doesn't work

a4aravind I have tried this snippet to automatically open the same url "google.com" in multiple tabs in Firefox. But it still only opens in new Windows. Since I only want to open that URL, I don't want that handle to switch back and forth. Can you help me find

Selenium doesn't work with Firefox or Chrome

Automate my work I'm trying to learn python web scraping, but I can't use selenium with any browser. from selenium import webdriver browser = webdriver.Firefox() This is all my code and I get this error. Traceback (most recent call last): File "C:\Users\tjh

Selenium doesn't work with Firefox or Chrome

Automate my work I'm trying to learn python web scraping, but I can't use selenium with any browser. from selenium import webdriver browser = webdriver.Firefox() This is all my code and I get this error. Traceback (most recent call last): File "C:\Users\tjh

Selenium doesn't work with Firefox or Chrome

Automate my work I'm trying to learn python web scraping, but I can't use selenium with any browser. from selenium import webdriver browser = webdriver.Firefox() This is all my code and I get this error. Traceback (most recent call last): File "C:\Users\tjh

Selenium doesn't work with Firefox or Chrome

Automate my work I'm trying to learn python web scraping, but I can't use selenium with any browser. from selenium import webdriver browser = webdriver.Firefox() This is all my code and I get this error. Traceback (most recent call last): File "C:\Users\tjh

Selenium doesn't work with Firefox or Chrome

Automate my work I'm trying to learn python web scraping, but I can't use selenium with any browser. from selenium import webdriver browser = webdriver.Firefox() This is all my code and I get this error. Traceback (most recent call last): File "C:\Users\tjh

Selenium doesn't open new URLs in new tabs (Python and Chrome)

Sam H123 I want to open a lot of URLs in different tabs using Selenium WebDriver and Python. I'm not sure what's wrong: driver = webdriver.Chrome() driver.get(url1) time.sleep(5) driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t') url2 = 'https

Selenium doesn't open new URLs in new tabs (Python and Chrome)

Sam H123 I want to open a lot of URLs in different tabs using Selenium WebDriver and Python. I'm not sure what's wrong: driver = webdriver.Chrome() driver.get(url1) time.sleep(5) driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL+'t') url2 = 'https

Selenium: New tabs not opening in the same browser in Chrome

Joe I'm trying to open a new tab in the same browser, but it doesn't seem to work. I am using Chrome version 58.0.3029.110 (64 bit) and Selenium 3.0.0. I used the following code: driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); Kuchar

Selenium drag and drop doesn't work in Chrome or Firefox

Attacker 76 I use the drag and drop code below to drag the picture into the light field. This works fine in InternetExplorer, but doesn't work in Firefox or Chrome. I don't understand why this is not done. As you can see in the code below, I have tried many di

Opening a new window with javascript doesn't work

User 2641103 I want to open a new window or tab pointing to a different url obtained from a link using javascript. Here is the code: <script> window.open($('#my_a_link_id').get(0).href); </script> However, nothing happened. What am I missing here? I h

Selenium 2.53.1 doesn't work on FireFox 48

open I am using selenium to test our website. When I build the project, there is an exception:- OpenQA.Selenium.WebDriverException: Could not start socket within 45000 ms. Trying to connect to the following address: 127.0.0.1:7055, the problem is with the code

Chrome keeps opening new tabs as new windows

username I have a problem with a new tab being opened as a new window. If you click the new tab button (next to an existing tab), or right-click and open in new tab, it briefly opens a new tab, but then switches to a new window. Any idea how to solve this prob

Chrome keeps opening new tabs as new windows

username I have a problem with a new tab being opened as a new window. If you click the new tab button (next to an existing tab), or right-click and open in new tab, it briefly opens a new tab, but then switches to a new window. Any idea how to solve this prob

JQuery doesn't work in Firefox but works in Chrome

Milojevic I'm having trouble with jQuery and Mozzila Firefox. Everything works fine in Chrome, but Firefox can't see jQuery. This is what I call jQuery <!-- Favicon and touch icons --> <link rel="shortcut icon" href="assets/ico/favicon.png"> <script

JQuery doesn't work in Firefox but works in Chrome

Milojevic I'm having trouble with jQuery and Mozzila Firefox. Everything works fine in Chrome, but Firefox can't see jQuery. This is what I call jQuery <!-- Favicon and touch icons --> <link rel="shortcut icon" href="assets/ico/favicon.png"> <script

.sort doesn't work in firefox but works in chrome

Mohit Harshan I have an array of objects like: [ {"type":"TEST", "submitted_on":null, "due_date": new Date(xxx) }, {"type": "ESSAY", "submitted_on": new Date(xxx), "due_date":new Date(xxx)}, {"type": "TEST", "submitted_on":new Date(xxx), "due_

jQuery doesn't work on Firefox but works on Chrome

Emma I have the following jQuerycode: function action(action){ event.preventDefault(); var products = $("#check-list input:checkbox:checked").map(function(){ return $(this).val(); }).get(); var string = JSON.stringify(products); var t

Scrollbar doesn't work in chrome but works in firefox

Niha The mouse wheel doesn't work when the website is opened in chrome, but when opened in firefox the mouse wheel works automatically.. I tried a lot of codes and tricks to change but it doesn't work Sagar It looks like one of the js files is conflicting with

JQuery doesn't work in Firefox but works in Chrome

Milojevic I'm having trouble with jQuery and Mozzila Firefox.Everything works fine in Chrome, but Firefox can't see jQuery. This is what I call jQuery <!-- Favicon and touch icons --> <link rel="shortcut icon" href="assets/ico/favicon.png"> <script