When we click on a link which open a new window, getting focus at currently opened windows is not straightforward in selenium. The main issue with it is that the new window may or may not have a WINDOWNAME that can be used by the webdriver function switch_to_window(WINDOWNAME).
If the new window has been opened by a code snippet such as,
<a href="http://www.qanepal.com/" target="_blank">Subscribe</a>
then the name is randomly assigned, and you’ll need to use code as shown below to find it’s name.
If you can modify the source html or influence the developers to do so, it would be nice to have a proper name assigned to the new window. This can be done by using a named target; eg:
<a href="http://some-link/" target="mytarget">some link</a>
Now, when the new window is opened, you can use it’s name explicitly in code, such as
If the new window has been opened by a code snippet such as,
<a href="http://www.qanepal.com/" target="_blank">Subscribe</a>
then the name is randomly assigned, and you’ll need to use code as shown below to find it’s name.
current_windows = self.driver.window_handles
# This is the code which open new windows.
self.driver.find_element_by_link_text("Subscribe").click()
new_windows = self.driver.window_handles
new_window = list(set(new_windows) - set(current_windows))[0]
self.driver.switch_to_window(new_window)
If you can modify the source html or influence the developers to do so, it would be nice to have a proper name assigned to the new window. This can be done by using a named target; eg:
<a href="http://some-link/" target="mytarget">some link</a>
Now, when the new window is opened, you can use it’s name explicitly in code, such as
driver.switch_to_window('mytarget')
Thanks admin.It is interesting to read.Keep share more about selenium.
ReplyDeleteRegards,
Selenium training | Selenium Course in Chennai | Best Selenium training institute in Chennai