简体   繁体   中英

Unable to focus on new tab in firefox when clicking on the link that opens a new tab using selenium and python

element=driver.find_element_by_xpath("html/body/footer/div/div[1]/section/div/div/div[2]/div[1]/ul/li[9]/div/div/a")
driver.execute_script("arguments[0].click();", element)
print(driver.title)

The xpath mentioned is that of a link. In the above code after driver.execute_script is executed the link is opened in a new tab but driver.title still shows the title of the old tab as a result a new element in the new tab could not be identified. Can someone help me here please.

Selenium version used: 3.11.0 Firefox version used: 47.0.2 geckodriver version used: 0.14

If after clicking on next button, Page opens in a new tab then you have to switch the focus of WebDriver to that window.

window_before = driver.window_handles[0]
# Click on next button on Page 1.
# Opens a new tab
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)

#performs some operations on Page 2.
print(driver.title)
driver.close()

driver.switch_to.window(window_before )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM