Selenium Switch Focus to Tab, Which Opened After Clicking Link

Clicking links in newly opened tab using WebDriver

You will need to use the .switchTo(windowHandle); command to access your second tab.

Before opening the second tab - get the windowHandle of the open tab:

String mainWindow = driver.getWindowHandle();

Then do your action that opens the second tab. Now you'll need to know the handle of the second tab and switch control to it:

Set<String> handles = driver.getWindowHandles();  
for (String handle : handles) {
if (!handle.equals(mainWindow)) {
driver.switchTo().window(handle);
break;
}
}

Your actions for the second tab will now happen in that second window. When you're finished and need to interact with the first tab again: driver.switchTo().defaultContent();

Switch tabs using Selenium WebDriver with Java

    psdbComponent.clickDocumentLink();
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
driver.close();
driver.switchTo().window(tabs2.get(0));

This code perfectly worked for me. Try it out. You always need to switch your driver to new tab, before you want to do something on new tab.

How to focus on second tab and work on it using selenium webdriver

you can do like this:

    Set<String> handles = driver.getWindowHandles();
String currentHandle = driver.getWindowHandle();
for (String handle : handles) {

if (!handle .equals(currentHandle))
{
driver.switchTo().window(handle);
}
}

//fill your form in the other tab
......

//go back to first tab if you want
driver.switchTo().window(currentHandle);

Selenium: open new link and switch tab to get content

You have to switch to the new tab using window handles. Read more from this page https://selenium-python.readthedocs.io/navigating.html#moving-between-windows-and-frames

driver.window_handles will give a list of window handles available.

driver.current_window_handle will print the current window handle.

Use driver.switch_to.window to switch to a window handle.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome('/path/to/chromedriver')
url="http://www1.fips.ru/wps/portal/!ut/p/c5/jY5bDkNAGIXX0hXMjyleh6QzlKnbKF5E0kZIXRJCa_VlAbTnPJ58Jx_K0Nq2mKqyGKuuLV4oQZmaWxbx2JVKcJNdAKJzQS62KgNV1j1Vc5MShjUHgPLABAsbWGbUkMFS_qFhJwR-0HeUAM7DWu_dz5g4izlF9eK_B8efh4VIPBLgcsd7xIEwiNFgO4hXJttej4y3_cCJs655ohRl2p4bDc-ob4SYejaX5HT6Ahe0tjA!/dl3/d3/L0lDU0lKSmdwcGlRb0tVUW9LVVFvS1VRIS9ZQVVJQUFJSUlJTU1JQ0tDRUFBRUFDR0lLQUdJT0JKQkpPQkZORk5PRkRMRExPREEvNEMxYjlXX05yMGdDVWd4RW1SQ1V3cE1oRXBSU1pHSlRpQSEhLzdfSUlBUEhLRzEwTzJNMDBBOE5VQUZKNjJHUzUvd2x5Snc5OTUzMDEwNi80MDk4Njg1NTc1NjcvbWF4TGV2ZWwvMS9ub2RlSWQvNTgvYWNOYW1lL2NsaWNrVHJlZQ!!/"
driver.get(url)
element=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,'//*[@id="mainContent"]/table/tbody/tr[1]/td/div[2]/table/tbody/tr[1]/td[2]/div[2]/div/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/div/table/tbody/tr/td/div[1]/table/tbody/tr[2]/td[2]/a')))
element.click()
element=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,'//*[@id="mainContent"]/table/tbody/tr[1]/td/div[2]/table/tbody/tr[1]/td[2]/div[2]/div/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/div/table/tbody/tr/td/div/ul/ul/ul/ul/ul/li[1]/a[2]')))
element.click()
#below opens in a new tab
element=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,'//*[@id="mainContent"]/table/tbody/tr[1]/td/div[2]/table/tbody/tr[1]/td[2]/div[2]/div/table/tbody/tr/td/table/tbody/tr/td[1]/table/tbody/tr/td/div/table/tbody/tr/td/table[2]/tbody/tr[1]/td[3]/a')))
element.click()
driver.switch_to.window(driver.window_handles[1])
element=WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,'//*[@id="NameDoc"]/b')))
print(element.text)

Output

ОПИСАНИЕ ИЗОБРЕТЕНИЯ К ПАТЕНТУ

The output is a text from the new tab. You can print the entire page source if you want.

How to handle new window, which opened automatically in new tab?

If it's a new window to count the number of WindowHandles you need to induce WebDriverWait for numberOfWindowsToBe(2) and you can use the following code block:

String mainWindowHandler = driver.getWindowHandle(); // store mainWindowHandler for future references
//line of code that opens a new TAB / Window
new WebDriverWait(driver, 5).until(ExpectedConditions.numberOfWindowsToBe(2)); //induce WebDriverWait
Set<String> handles = driver.getWindowHandles();
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext())
{
String subWindowHandler = iterator.next();
if (!mainWindowHandler.equalsIgnoreCase(subWindowHandler))
{
driver.switchTo().window(subWindowHandler);
}
}

References

You can find a couple of relevant detailed discussions in:

  • Selenium switch focus to tab, which opened after clicking link
  • Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium
  • org.openqa.selenium.WebDriverException: invalid argument: 'handle' must be a string while window handling with Selenium and Java in Linux


Related Topics



Leave a reply



Submit