Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium webdriver using switch_to_windows() and printing the title doesn't print the title.

Here is the code

for handle in browser.window_handles:
    print "Handle = ",handle
    browser.switch_to_window(handle);
    elem = browser.find_element_by_tag_name("title")
    print elem.get_attribute("value")

I am getting the following output

Handle =  {564f8459-dd20-45b8-84bf-97c69f369738}
None
Handle =  {85338322-5e58-4445-8fe3-3e822d5a0caf}
None

After getting the handle I switch to the window and print the title. Why am I not seeing any title. Won't there be any titles? When I see the html source for the page I see the title tag though.

like image 427
Anand Avatar asked Oct 28 '12 23:10

Anand


People also ask

How do you get the title in Selenium?

We can obtain the page title using Selenium webdriver. The method getTitle() is used to obtain the present page title and then we can get the result in the console.

Which statement will give the title of the page in Selenium?

Syntax. t = driver. getTitle(); Let us find the title of the current page.

What are the methods to get and print the text from webpage in Selenium?

We can get the text from a website using Selenium webdriver USING the getText method. It helps to obtain the text for a particular element which is visible or the inner text (which is not concealed from the page).

Does Selenium wait for page to load?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.


2 Answers

driver.switch_to_window(driver.window_handles[-1])
title=driver.title

You can do it simply use the code above. driver.window_handles[-1] would get the lastest window.

like image 138
Moxi Zhong Avatar answered Oct 17 '22 13:10

Moxi Zhong


The title of the page wouldn't be in a value attribute of a title element, it would be the text contents of that element.

The correct way to access that text would be browser.find_element_by_tag_name("title").text

Or even easier, just access browser.title.

like image 41
Acorn Avatar answered Oct 17 '22 12:10

Acorn