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