0

I have a URL http://travel.aol.co.uk/2016/03/23/best-hotels-for-families-mediterranean/ which has some Social sharing links such as Twitter , Facebook etc. When I click on them they lead to a new window. However, I am not able to perform operations on that window as I cannot switch to that window. Any help is appreciated. I tried switchTo methods but they don't work. Any help is appreciated.

Following is the code

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get('http://travel.aol.co.uk/2016/03/23/best-hotels-for-families-     mediterranean/')
window_before = driver.window_handles[0]
linkElement = driver.find_element_by_class_name('share-icon-twitter')
linkElement.click()
window_after = driver.window_handles[1]
driver.switch_to_window(window_after)
print (driver.title)

When I print the title at the end, nothing shows up. But if I switch back to the old window and then print the title, correct title shows up.

Thank you.

1 Answers1

0

If your requirement is shifting between the windows, You can achieve it by switch_to_window method. And also window_handles which will help to obtain the windows.

    from selenium import webdriver
    from selenium.webdriver.common.keys import Keys

    driver = webdriver.Firefox()
    driver.get(url)
    first_window = driver.window_handles[0]
    linkitem = driver.find_element_by_id("id")
    linkitem.click()
    second_window = driver.window_handles[1]
    driver.switch_to_window(second_window)

if you want to get back to the first window you can use

     driver.switch_to_window(first_window)

Hope that helps. Thank You

Strik3r
  • 1,052
  • 8
  • 15
  • Hi San, tried your suggestion, but at the end of the last line when I tried to print the page title, it didn't print anything. Following is the code. – Vishwanath R Kulkarni Apr 20 '16 at 06:28
  • from selenium import webdriver from selenium.webdriver.common.keys import Keys driver = webdriver.Firefox() driver.get('http://travel.aol.co.uk/2016/03/23/best-hotels-for-families-mediterranean/') window_before = driver.window_handles[0] linkElement = driver.find_element_by_class_name('share-icon-twitter') linkElement.click() window_after = driver.window_handles[1] driver.switch_to_window(window_after) – Vishwanath R Kulkarni Apr 20 '16 at 06:29
  • hii @VishwanathRKulkarni How did u tried to print the page title? I tried using the following Code and it's working fine for me title=driver.find_element_by_xpath(r'\html\head\title') . It didnt printed the actual text but is able to locate the title and is printing the element id. – Strik3r Apr 20 '16 at 08:00