2

I'm writing a test which goes through multiple pages and then verifies the title is being displayed. Currently my test is at the point where i can click a button and it opens a report. The problem is the report opens up in a new tab so i need my test to move tabs and verify the title in the new tab.

The title that needs to be verified is 'valuation'. I've done an assert to confirm the title is the same as i expect

I've written the code below in python. Its currently failing at wait on the 2nd line

current = self.driver.current_window_handle
wait(self.driver, 10).until(EC.new_window_is_opened(self.driver.window_handles))
self.driver.switch_to.window([w for w in self.driver.window_handles if w != current][0])
title = self.driver.title
self.assertTrue("Valuation" == self.driver.title)

I'm opening a new tab with the following lines of code:

element = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']") 
driver.execute_script("arguments[0].click();", element)
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
PythonCoder4_09
  • 67
  • 2
  • 10

1 Answers1

2

new_window_is_opened(current_handles)

new_window_is_opened(current_handles) is the expectation that a new window will be opened and have the number of windows handles increase.


Demonstration

The following example opens the url http://www.google.co.in first and then opens the url https://www.yahoo.com in the adjacent tab:

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("http://www.google.co.in")
windows_before  = driver.window_handles
driver.execute_script("window.open('https://www.yahoo.com')")
WebDriverWait(driver, 20).until(EC.new_window_is_opened(windows_before))
driver.switch_to.window([x for x in driver.window_handles if x not in windows_before][0])

Note: The argument passed to new_window_is_opened(current_handles) is a list. In order to create a list we need to use windows_before = driver.window_handles


This usecase

In your code block, it is expected that when you:

current = self.driver.current_window_handle

There exists only one window handle. So moving forward, the line of code:

wait(self.driver, 10).until(EC.new_window_is_opened(self.driver.window_handles))

would wait for a new window to be opened and have the number of windows handles increases and that would happen only after an action is performed which opens a new window, which seems to be missing from your code block.


Solution

Insert the line of code which initiates opening of a new :

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

windows_before  = driver.window_handles
element = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']")
driver.execute_script("arguments[0].click();", element)
WebDriverWait(self.driver, 10).until(EC.new_window_is_opened(windows_before))
driver.switch_to.window([x for x in driver.window_handles if x not in windows_before][0])
assertTrue("Valuation" == driver.title)

Note: As per the lines of code you have updated within your comments you are not using Python class, so you shouldn't use the keyword self.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • So this it the line before which clicks the button which then opens the new tab. Are you saying i should inset these two lines into the second line of your code? `element = driver.find_element_by_xpath("//input[@id='save' and @name='save'][@value='View Report']") driver.execute_script("arguments[0].click();", element)` – PythonCoder4_09 Jun 26 '20 at 14:49
  • @PythonCoder4_09 Yeah, that's what is expected. However, the _xpath_ seems the one I constructed for you in one of my answers for you earlier today, where as I will highly discourage `driver.execute_script()` unless you have a genuine z-axis issue. – undetected Selenium Jun 26 '20 at 14:52
  • Thanks, it's failing on the first line with this error `current = self.driver.current_window_handle AttributeError: module 'self' has no attribute 'driver'` @DebanjanB – PythonCoder4_09 Jun 26 '20 at 14:58
  • @PythonCoder4_09 My bad, there was a typo, fixed it now. – undetected Selenium Jun 26 '20 at 14:59
  • Driver is throwing this problem Cannot find reference 'driver' in '__init__.py' – PythonCoder4_09 Jun 26 '20 at 15:09
  • This is the error message along with the above current = self.driver.window_handles AttributeError: module 'self' has no attribute 'driver' @DebanjanB – PythonCoder4_09 Jun 26 '20 at 15:09
  • @PythonCoder4_09 Checkout the updated answer and let me know the status. – undetected Selenium Jun 26 '20 at 17:00
  • Few issues. `driver.find_element` is saying unresolved reference even after i've imported the package. Secondly on line 2 before `driver.execute` there an end of statement expected. And thirdly `assertTrue` is an unresolved statement – PythonCoder4_09 Jun 29 '20 at 08:39
  • @PythonCoder4_09 `execute_script()` and `assertTrue()` isn't a part of the solution I'm offering. I trust you inherited those from your previous questions. – undetected Selenium Jun 29 '20 at 08:45