Selenium2+python Automation-Processing Browser popup (reprint)

Source: Internet
Author: User

This post goes from blog: Shanghai-Little T

Original address: http://blog.csdn.net/real_tino/article/details/59068827

When we browse the Web page, we often encounter various patterns of pop-up windows, in the UI Automation testing is bound to deal with these pop-up windows, here is the front end of the two types of window processing methods.

First, alert pop-up window

This kind of pop-up window is the simplest one, selenium has its own way to deal with it, using Switch_to.alert to navigate to the pop-up window, and then use a series of methods to operate:

    • Accept-click the "Confirm" button
    • Dismiss-click the "Cancel" button (if there is a button)
    • Send_keys-Enter content (if there is an input box)

Here is an example of a rookie tutorial: Http://www.runoob.com/try/try.php?filename=tryjs_alert, click "Show Warning Box" on the left side of the page to bring up an alert pop-up window:

We can use the following code to switch to the pop-up window and click the "OK" button effect:

al = driver.switch_to_alert()al.accept()

Here this switch_to_alert () is actually the old wording, supposedly should be used Switch_to.alert (), but the new wording will be error, the current speculation is the version of the problem, may not support the new wording, here first use the old wording.

The following is the complete code, in order to run the time to see clearly, I added two places to wait:

# encoding : Utf-8from Selenium Import webdriverimport timedriver = Webdriver.get ( "http://www.runoob.com/try/ Try.php?filename=tryjs_alert ") Driver.switch_to . FRAME ( "Iframeresult") Driver.find_element_by_xpath ( Span class= "hljs-string" > "Html/body/input") .click () time .sleep (1) Al = Driver.sleep (1) Al.accept ()            

Second, custom pop-up window
Because the alert pop-up window is not beautiful, now most of the website will use custom pop-up window, use selenium own method can not control, this time will move out JS Dafa. Here is an example of the homepage of the New World Education website:

As you can see, this kind of pop-up window is now the mainstream manifestation, processing this pop-up window can take advantage of the HTML DOM Style object, there is a display property, you can set how the element is displayed,

The detailed explanation can refer to http://www.w3school.com.cn/jsref/prop_style_display.asp. Setting the display value to none will remove the pop-up window:

js = ‘document.getElementById("doyoo_monitor").style.display="none";‘

The complete code is as follows:

# encoding:utf-8from selenium import webdriverimport timedriver = webdriver.Firefox()driver.get("http://sh.xsjedu.org/")time.sleep(1)js = ‘document.getElementById("doyoo_monitor").style.display="none";‘driver.execute_script(js)

is not simple and efficient ~

Selenium2+python Automation-Processing Browser popup (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.