15

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.

4

2 回答 2

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

您只需使用上面的代码即可。driver.window_handles[-1] 将获得最新的窗口。

于 2013-03-11T05:30:02.017 回答
11

页面的标题不会在元素的value属性中title,而是该元素的文本内容。

访问该文本的正确方法是browser.find_element_by_tag_name("title").text

或者更简单,只需访问browser.title.

于 2012-10-28T23:14:12.407 回答