stay WebDriver We have to deal with JS Generated alert、confirm as well as prompt, Need to be used switch_to_alert() Locate the alert/confirm/prompt, And then use text、accept()、dissmiss()、send_keys() And so on .

text: get alert/confirm/prompt Text messages in

accept(): Accept warning box

dissmiss(): Close warning box

send_keys(): Send a text message to the warning box

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
dr = webdriver.Chrome()
dr.maximize_window()
dr.implicitly_wait(10)
dr.get('https://www.baidu.com') sz = dr.find_element_by_link_text(' Set up ')# The mouse hovers over the settings button
ActionChains(dr).move_to_element(sz).perform() dr.find_element_by_link_text(' Search settings ').click()
sst = dr.find_element_by_class_name('prefpanelgo')# Save settings button
sst.send_keys(Keys.ENTER) dr.switch_to_alert().send_keys()
dr.close()

The code is on baidu home page , Hover over the settings button , Then open the search settings , After clicking the Save button , Accept the warning box that pops up .

use Chrome When you bring your own developer tools , Directly use the positioning button to click the search settings under settings , The drop-down menu won't come out , You can click the search settings in the drop-down menu first , Then use the shortcut key of the positioning button :Ctrl+Shift+C To locate

When you click the Save Settings button , Use it directly click() Will report a mistake , Something like the code above , Through the keyboard Enter Press the button instead of . The other way is time.sleep() Seconds , And then again click(). It's like there's a transparent div The reason to load .

switch_to_alert(),PyCharm Hint that this is not the latest method , The latest is switch_to.alert(), But the latest one seems to have no response .

python3+selenium introduction 12- The warning box handles more related articles

  1. selenium Basics ( Handling of warning box )

    selenium Basics ( Handling of warning box ) stay webdriver In dealing with JavaScript There are three types of warning boxes generated alert confirm prompt The way to go to the warning box is :driver.switc ...

  2. Selenium3 + Python3 Automated testing series 8 —— Warning box processing and drop-down box selection

    Warning box handling stay WebDriver In dealing with JavaScript The generated alert.confirm as well as prompt Very simple , The specific way is to use switch_to.alert Method to alert/confi ...

  3. python3+selenium introduction 01- Environment building

    As a test , There should be a clear feeling in the last two years . That's why it's hard to find a job , The demands are getting higher , automated testing , Performance test, etc . No automated testing capability , It's hard to find a job if you only know how to do it , The salary is not high . So we still need to learn some techniques . First, learn a programming language ...

  4. python3+selenium introduction 14- Upload and download files

    One way to upload files is by locating input label , And then use send_keys() Method to pass in the path of the file to be uploaded . The other is to use a third-party plug-in to upload files . Look at the below imput How to label . You can check it yourself . <!DOC ...

  5. python3+selenium introduction 11- Window switch

    When opening a new browser window , If you want to locate the elements of a new window , You need to switch to this newly opened window first , To navigate to the elements under the window . current_window_handle: Get the current handle . The handle can be understood as the ID card of the window wi ...

  6. python3+selenium introduction 09- Keyboard events

    Keys Class provides methods for almost all keys on the keyboard . adopt send_keys() Methods can not only be used to simulate keyboard input , You can also use it to simulate keys on the keyboard . Including key combinations , such as Ctrl+a etc. Import required Keys class from selen ...

  7. python3+selenium introduction 05- Element operation and common methods

    After learning element positioning , Look at the operation of some elements , There are also some common methods clear() Clear the input box click() Click on send_keys() Keyboard entry import time from selenium imp ...

  8. python3+selenium introduction 04- Element localization

    When we operate the browsing interface , Like clicking on a button , Search box input content . You need to move the mouse over , Then click , Or input content . stay selenium It's the same with the operation . You need to position the elements first , Then you can do it . With the help of browser developers ...

  9. python3+selenium introduction 02- Operate the Firefox browser

    Use selenium When you open your browser , Need to go first selenium Download a corresponding browser from the official website driver plug-in unit .selenium download . The official website may not open abroad . Or you can download it from my Internet disk , But it may not be the latest web link . take ...

Random recommendation

  1. JavaScript Simple Paging , compatible IE6,~3KB

    brief introduction compatible IE6+ And the simple pagination of modern browsers , Support multiple pagination on the same page . Use Browser <link rel="stylesheet" href="css/GB-pa ...

  2. CoreData Database analysis

    Core Data yes iOS5 And then there was a framework , It provides objects - Relation mapping (ORM) The function of , That is, to be able to OC Object into data , Save in SQLite In the database file , It can also restore the data stored in the database to OC object . Here is the number ...

  3. PHPExcel Translated version of Chinese development manual (1)

    Please note that this is a rough translation , For reference only , It's not a fine print It will be updated later in the final edition English original online document https://github.com/PHPOffice/PHPExcel/wiki/User%20Documenta ...

  4. Jquery Learning one

    One .jQuery summary 1.Javascript The code base It was used in early project development Javascript Native code , Write line by line . however Javascript Self existence 3 A drawback : ① complex DOM operation ② atypism ...

  5. Hadoop Log analysis .

    http://www.ibm.com/developerworks/cn/java/java-lo-mapreduce/

  6. Sharing is very useful Java Program ( Key code )( 8、 ... and )---Java InputStream Read network response Response Method of data !( important )

    original text : Sharing is very useful Java Program ( Key code )( 8、 ... and )---Java InputStream Read network response Response Method of data !( important ) Java InputStream Read data problem ======== ...

  7. Create transparency CEdit Control

    1. In the resource editor Edit The control of Border Set to none( Of course, you can also modify the window style in the code ) 2. Under heavy load CEdit Response in the class of MSG_OCM_CTLCOLOREDIT news , Set the background mode to transparent in the handler ...

  8. Git in pull and clone The difference between

    git pull git clone clone It's not local repository when , Remote repository Download the whole thing . pull It's local repository when , Remote reposi ...

  9. Android Medium Service And Thread The difference between [ turn ]

    A lot of times , You may ask , Why use Service, without Thread Well , Because with Thread It's very convenient , Compared with Service It's much more convenient , Now I'll explain it in detail . 1). Thread:Thre ...

  10. IntelliJ Use guide —— Import Eclipse Of Web project

    Usually someone in a team may use eclipse, Someone uses it intelliJ, So often there will be the need to import other people to use eclipse Built web project . and IntelliJ Provides a variety of project types to import , Among them eclipse. stay ...