程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python selenium pop up processing operation guide

編輯:Python

Catalog

HTML The pop-up window in

Selenium Positioning pop-up window

alert

confirm

prompt

Other methods

Determine the pop-up box

expand

Dialog box

summary

HTML The pop-up window in

1、 In a real system , When some operations are completed, a dialog box will pop up to prompt , It is mainly divided into " Warning message box "," Confirmation message box "," Prompt message dialog " Three types of dialog boxes

2、 Warning message box :alert

⑴ The warning message box provides a " determine " The button lets the user close the message box , And the message box is a modal dialog , That is to say, the user must close the message box before continuing the operation

3、 Confirmation message box (confirm)

    ⑴ The confirmation message box prompts the user with " Yes or no " problem , Users can choose " determine " Button and " Cancel " Button

4、 Prompt message dialog (prompt)

⑴ The prompt message box provides a text field , Users can enter an answer in this field in response to your prompt

       ⑵ The message box has a " determine " Button and a " Cancel " Button . choice " confirm " Will respond to the corresponding prompt message , choice " Cancel " The dialog box is closed

Selenium Positioning pop-up window

1、 stay Selenium If you simply locate the pop-up window , It can't be located

2、 Because of this Pop ups do not belong to HTML The elements of the , It belongs to the pop-up window of the browser ( By JavaScript Generated ), Therefore, the method of locating elements cannot be used to locate

    ⑴ Such elements are used F12 When selecting elements , You can't choose

3、Selenium Medium WebDriver The object provides switch_to_alert() Method orientation ( Capture ) To all kinds of pop ups (alert、confirm、prompt)

4、WebDriver There are mainly the following methods when handling pop ups for objects :

    ⑴switch_to_alert(): Locate the pop-up dialog box

    ⑵text: Get dialog text value

    ⑶accept(): It's like clicking " confirm "

    ⑷dismiss(): It's like clicking " Cancel "

    ⑸send_keys(): The input values , This method can only be used in prompt Class used in the bullet box

5、 The basic idea : First use the method switch_to_alert() Locate the alert Wait for the pop-up box , And then deal with it accordingly ( confirm 、 Cancel 、 The input values )

    ⑴ Be careful : For the three types of pop-up boxes, the method of locating pop-up boxes is the same

alert

1、alert Class pop-up box should be one of the most frequently encountered pop-up boxes in practice

example 1:

import timefrom selenium import webdriver# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter Baidu home page driver.get('https://www.baidu.com/')# Enter Baidu settings page driver.find_element_by_xpath("//*[@id='s-usersetting-top']").click()# open " Search settings "driver.find_element_by_xpath("//*[@id='s-user-setting-menu']/div/a[1]").click()time.sleep(1)# Click Save settings driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]').click()# This will pop up alter Class dialog , Use switch_to_alert() Locate the pop-up window alter = driver.switch_to_alert()print(alter)print(driver.switch_to.alert)# Print the contents of the dialog box print(alter.text)# Click on the dialog box [ determine ] Button to close the dialog box alter.accept()time.sleep(2)# Close the dialog box and continue to operate the page driver.find_element_by_id("kw").send_keys(" Mice that are not afraid of cats A")"""<selenium.webdriver.common.alert.Alert object at 0x000001EDE7BBA7F0><selenium.webdriver.common.alert.Alert object at 0x000001EDE7BC8D68> Your preferences have been recorded """

notes :

1、 Write... In the editor switch_to_alert() Method, a horizontal line appears ( Delete line ), This means that the method is too old , It may be abandoned later

    ⑴ So we can also use switch_to.alert Method

2、 There is a slight gap between the two methods

    ⑴switch_to_alert(): Is a class method

    ⑵switch_to.alert: It's a class property

    ⑶ But both return values are the same : It's all one alter object

3、 The bullet box must be in the current Selenium Point to... In the window ( On the same page ), So after closing the pop-up box , You can continue to operate the elements of this page

confirm

1、 This kind of pop-up box is also common , But there is no similar pop-up box on the Internet , Just wrote a demo

example 2:

<html><head><script type="text/javascript"> function show_confirm() { var r=confirm(" Excuse me, : Have you followed my blog ?"); }</script></head><body> <center> <input id="anjing" type="button" onclick="show_confirm()" value=" Am I , I have a surprise !" /> <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> </center></body></html>

example 3:

import timefrom selenium import webdriver# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter the page driver.get('C:\\Py_Demo\\Demo\\Py_Project\\demo.html')# Click the button and a dialog box will pop up driver.find_element_by_id("mouse").click()time.sleep(1)# Positioning dialog alter = driver.switch_to.alertprint(alter)# Print the contents of the dialog box print(alter.text)# Click on the dialog box [ determine ] Button to close the dialog box # alter.accept()# Click on the dialog box [ Cancel ] Button to close the dialog box alter.dismiss()time.sleep(1)# Close the dialog box and continue to operate the page driver.find_element_by_id("kw").send_keys(" Mice that are not afraid of cats A")"""<selenium.webdriver.common.alert.Alert object at 0x0000028242C68DA0> Excuse me, : Have you followed my blog ?"""prompt

1、 This kind of pop-up box should be few , I can't find a similar pop-up box on the Internet , Just wrote a demo

example 4:

<html><head><script type="text/javascript"> function disp_prompt() { var name=prompt(" Excuse me, : Have you followed my blog ?") }</script></head><body> <center> <input id="mouse" type="button" onclick="disp_prompt()" value=" Am I , I have a surprise " /> <input id="kw" name="wd" class="s_ipt" value="" maxlength="255" autocomplete="off"> </center></body></html>

example 5:

import timefrom selenium import webdriver# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter the page driver.get('C:\\Py_Demo\\Demo\\Py_Project\\demo.html')# Click the button and a dialog box will pop up driver.find_element_by_id("mouse").click()time.sleep(1)# Positioning dialog alter = driver.switch_to.alertprint(alter)# Print the contents of the dialog box print(alter.text)# Send text to dialog alter.send_keys(" Mice that are not afraid of cats A")time.sleep(2)# Click on the dialog box [ determine ] Button to close the dialog box # alter.accept()# Click on the dialog box [ Cancel ] Button to close the dialog box alter.dismiss()time.sleep(1)# Close the dialog box and continue to operate the page driver.find_element_by_id("kw").send_keys(" Mice that are not afraid of cats A")""" notes : In this case, I feel send_keys() It doesn't work ( Not sent to the input box ), do not know why <selenium.webdriver.common.alert.Alert object at 0x0000018EDC058E48> Excuse me, : Have you followed my blog ?""" Other methods

1、 The positioning pop-up box can not only use the above "switch_to.alert" and "switch_to_alert()" Method

2、 There are other ways to jump to alert

    ⑴ I need to use Alert modular , First, import from selenium.webdriver.common.alert import Alert 

example 6:

import timefrom selenium import webdriverfrom selenium.webdriver.common.alert import Alert# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter Baidu home page driver.get('https://www.baidu.com/')# Enter Baidu settings page driver.find_element_by_xpath("//*[@id='s-usersetting-top']").click()# open " Search settings "driver.find_element_by_xpath("//*[@id='s-user-setting-menu']/div/a[1]").click()time.sleep(1)# Click Save settings driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]').click()# This will pop up alter Class dialog , Use Alert Locate the pop-up window time.sleep(2)alter = Alert(driver)# Print the contents of the dialog box print(alter.text)# Click on the dialog box [ determine ] Button to close the dialog box alter.accept()time.sleep(2)# Close the dialog box and continue to operate the page driver.find_element_by_id("kw").send_keys(" Mice that are not afraid of cats A")

notes :

1、alter = Alert(driver): So in the current browser page (WebDriver Under the object ) Instantiated a alter object

Determine the pop-up box

1、 During code execution , There may be other factors , The pop-up box does not appear due to slow network speed , We can operate by judging whether the pop-up box appears in the element

2、 The specific method is to use Selenium Medium " According to wait " And judgment " Whether the broken element exists " Methods
    ⑴ As for these two knowledge points , It will be introduced separately later , Just get to know it here

example 7:

import timefrom selenium import webdriverfrom selenium.webdriver.support.wait import WebDriverWaitfrom selenium.webdriver.support import expected_conditions as EC# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter Baidu home page driver.get('https://www.baidu.com/')# Enter Baidu settings page driver.find_element_by_xpath("//*[@id='s-usersetting-top']").click()# open " Search settings "driver.find_element_by_xpath("//*[@id='s-user-setting-menu']/div/a[1]").click()time.sleep(1)# Click Save settings driver.find_element_by_xpath('//*[@id="se-setting-7"]/a[2]').click()# This will pop up alter Class dialog , Use Alert Locate the pop-up window WebDriverWait(driver, 5).until(EC.alert_is_present()) # wait for 5 Seconds later, judge whether the pop-up window appears """ Be similar to time.sleep(5)result = EC.alert_is_present()(driver) # Determine whether there is a dialog box , The writing here needs attention print(result)if result: passelse: print(" No dialog box pops up ")"""alter = driver.switch_to.alert# Print the contents of the dialog box print(alter.text)# Click on the dialog box [ determine ] Button to close the dialog box alter.accept()time.sleep(2)# Close the dialog box and continue to operate the page driver.find_element_by_id("kw").send_keys(" Mice that are not afraid of cats A") expand Dialog box

1、 Dialog box : It is not belong to HTML The elements of the , It belongs to the browser itself , You can't locate with elements

2、 In practice, we must distinguish whether it is a dialog box

3、 For example, click on [ Sign in ] Button , A small panel will pop up ( Pages above the web level ). This is not a dialog . It's part of the web page itself , This can be located using elements

    ⑴ For example, in the previous example " The setting panel of Baidu home page ": Click on [ Set up ] A small panel pops up after , This can be located

example 8: Log in to chalk

import timefrom selenium import webdriver# Get browser object driver = webdriver.Chrome()# Set the browser window size driver.maximize_window()# Enter the chalk net driver.get('https://fenbi.com/page/home')# Click the login button driver.find_element_by_xpath('//*[@id="headercontent"]/div[2]/div/div[1]').click()# Enter account driver.find_element_by_xpath('//*[@id="fenbi-web-header"]/fb-header/div[2]/div/div[2]/div[1]/input').send_keys(" account number ")# Input password driver.find_element_by_xpath('//*[@id="fenbi-web-header"]/fb-header/div[2]/div/div[2]/div[2]/input').send_keys(" password ")# Click the login button driver.find_element_by_xpath('//*[@id="fenbi-web-header"]/fb-header/div[2]/div/div[2]/div[4]/div/div').click() summary

This is about Python Selenium This is the end of the pop-up processing article , More about Python Selenium Please search the previous articles of SDN or continue to browse the related articles below. I hope you will support SDN more in the future !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved