Selenium2+python Automation-Window multi-label processing method Summary (reprint)

Source: Internet
Author: User
Tags setf

This post goes from blog: Shanghai-Little T

Original address: Https://i.cnblogs.com/EditArticles.aspx?opt=1

When we encounter multiple browser windows or multiple tabs (tab) in a single browser with selenium, we tend to be less than good at handling them, and here are two ways to handle them.

The example quoted in this article is Baidu, there is a "Baidu homepage" hyperlink at the bottom of Baidu home page, click on it will be another new tab, is a good example:

1, Switch_to.window ()

This method is a built-in method in Webdriver, in addition to switching in multiple browsers, a single browser multiple tabs (tab) between the switch can also use this method, the idea is to get all the label's handle, and then loop through and determine whether it is the current tab, if not the switch. The detailed code is as follows:

# Encoding:utf-8

From selenium import Webdriver

Driver = Webdriver. Firefox ()

Driver.get ("http://www.baidu.com")

driver.find_element_by_id ("SETF"). Click ()

Handles = Driver.window_handles

For handle in handles:

If Driver.current_window_handle! = handle:

Driver.switch_to.window (handle)

Driver.find_element_by_link_text ("Baidu homepage"). Click ()

The last sentence click on the "Baidu Home" link on the new tab to successfully represent the successful switch tag.

2. Use JS to clear the target value

To view the hyperlink with the F12 tool, you can see that there is a target property:



Target=_blank means to open the linked document in a new window (quoted from W3school), the popular point is that the hyperlink will be opened with a new window (new tab), then the value will be cleared to achieve a click on the hyperlink will not be another new tab page. Use the Document object method to find the ID and then clear the value of its target:

JS = ' document.getElementById ("setf"). target= "";

JS after writing to execute it, you can achieve the effect. The complete code is as follows:

# Encoding:utf-8

From selenium import Webdriver

Driver = Webdriver. Firefox ()

Driver.get ("http://www.baidu.com")

JS = ' document.getElementById ("setf"). target= "";

Driver.execute_script (JS)

driver.find_element_by_id ("SETF"). Click ()

Driver.find_element_by_link_text ("Baidu homepage"). Click ()

The last sentence click on the "Baidu Home" link on the new tab to successfully represent the successful switch tag.

The HTML DOM defines a variety of methods for finding elements, in addition to getElementById (), Getelementsbyname () and getElementsByTagName (), in HTTP// There is detailed usage on the www.w3school.com.cn/jsref/dom_obj_document.asp.

Selenium2+python Automation-Window multi-label processing method Summary (reprint)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.