0
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

  • Possible duplicate of [getWindowHandles() not working in firefox 58.The focus remains on parent tab and does not transfer to next tab](https://stackoverflow.com/questions/49142180/getwindowhandles-not-working-in-firefox-58-the-focus-remains-on-parent-tab-and) – undetected Selenium Apr 16 '18 at 14:11

1 Answers1

0

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 )
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • tried your suggestion but got error tabs = driver.getWindow_Handles AttributeError: 'WebDriver' object has no attribute 'getWindow_Handles' – Bikash Mandal Apr 16 '18 at 11:30
  • No it didn't work. The thing is the code I have shared worked fine with Chrome but not in Firefox. In Chrome it is able to switch the tab to the new one but in Firefox it is failing. – Bikash Mandal Apr 16 '18 at 12:25
  • elm=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();", elm) driver.switch_to_window(driver.window_handles[1]) print(driver.current_url) – Bikash Mandal Apr 16 '18 at 12:34
  • I tried the above code and driver.current_url is returning "about:blank" – Bikash Mandal Apr 16 '18 at 12:35
  • Because "html/body/footer/div/div[1]/section/div/div/div[2]/div[1]/ul/li[9]/div/div/a this Xpath has some serious issue . – cruisepandey Apr 17 '18 at 05:36