Selenium + Python Warning box handling

Source: Internet
Author: User

This is a few days of learning Webdriver's warning box, specifically using the Switch_to_alert () method to navigate to Alert/confirm/prompt and then using Text/accept/dismiss/send_ Keys and other methods to operate.

But has been stuck in the acceptance warning box This step, in the book is given an example of Baidu homepage Search Settings pop-up window, but after the code runs, always in error:selenium.common.exceptions.NoSuchElementException: Message:unable to locate element: Search settings ("Search Settings" here may be other targeting information). Later think, can Baidu home has something changed, I try to do the same on the search page, the results can be!

Attached code:

From selenium import Webdriver

From Selenium.webdriver.common.action_chains import Actionchains

Import time

Driver = Webdriver. Firefox ()

Driver.implicitly_wait (10)

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

driver.find_element_by_id ("kw"). Send_keys ("Selenium")

driver.find_element_by_id ("su"). Click ()

#鼠标悬停至 "Settings" link

#setting = Driver.find_element_by_css_selector ('. PF ')

setting = Driver.find_element_by_link_text ("Settings")

Actionchains (Driver). Move_to_element (setting). Perform ()

#打开搜索设置

Driver.find_element_by_link_text ("Search Settings"). Click ()

#driver. Find_element_by_class_name ("Setpref"). Click ()

Time.sleep (2)

# Save Settings

Driver.find_element_by_class_name (' Prefpanelgo '). Click ()

Time.sleep (2)

#接受警告框

Driver.switch_to_alert (). Accept ()

Driver.quit ()

Selenium + Python Warning box handling

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.