Python3 Selenium automated web testing ==> advanced applications WebDriver IX - select operations and alert

Learning objectives:

  Conventional positioning method to master page elements


Scenes:

  Conventional methods and select web exclusive method of normal drop-down box select the elements

 

Official steps:

step1: select page elements positioned to conventional thinking

Processing HTML code screenshot

 

# - * - Coding: UTF-8 - * - 
from the Selenium Import webdriver
 Import Time
 from selenium.webdriver.common.action_chains Import ActionChains
 # routine select method: Baidu Home Settings search results show the number of entries 
url = " HTTPS: // the WWW. Baidu.com " 
DR = webdriver.Chrome () 
dr.get (URL) 
# dr.find_element_by_link_text ( 'set') .click () 
# dr.find_element_by_class_name (" setpref "). the Click () 
# the time.sleep (. 1) 
# . dr.find_element_by_xpath ( '// * [@ ID = "NR"] / Option [. 3]') the click () 
# conventional method two: the mouse to move the suspension to set search settings button clicks
= dr.find_element_by_link_text Mouse ( ' set ' ) 
ActionChains (DR) .move_to_element (Mouse) .perform () 
dr.find_element_by_class_name ( " setpref " ) .click () 
the time.sleep ( . 1 ) 
dr.find_element_by_xpath ( ' // * [ the above mentioned id = @ "nr"] / the Option [3] ' ) .click () 

the time.sleep ( 3 ) 
dr.Close () # use Close is to exit the current window, quit is to abandon the entire process dr

  

step2: Select proprietary methods, i.e. selenium built-in method

Processing HTML code screenshot

 

 solution:

# -*-  coding:utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select
import time

url = 'https://www.baidu.com'
dr = webdriver.Chrome()
dr.get(url)
dr.find_element_by_link_text('设置').click()
dr.find_element_by_class_name("setpref").click()
time.sleep(1)
#method_1
s1 = dr.find_element_by_id('nr')
Select(s1).select_by_visible_text('每页显示50条')
time.sleep(3)

#method_2
s2 = dr.find_element_by_id('nr')
Select(s2).select_by_value('20')
time.sleep(3)

#method_3
s3 = dr.find_element_by_id('nr')
Select(s3).select_by_index('0')
time.sleep(1)

dr.quit()

  

step3: processing alert pop-up box

Screenshot deal with situations, you need to click on the "OK" button, but the button no corresponding html element

 

solution:

# - * - Coding: UTF-8 - * - 
from the Selenium Import webdriver
 from selenium.webdriver.common.action_chains Import ActionChains
 from selenium.webdriver.support.select Import the Select
 Import Time
 "" " 
If you have a pop-up box pops up that you need to enter content , it is necessary to use the send_keys positioning element () to the input content 
determined by pop-up box may F12 to view the page elements, if there is a corresponding HTML element, normal position, but needs to handle the switching operation 
"" " 
URL = ' https://www.baidu.com ' 
DR = webdriver.Chrome () 
dr.get (URL) 
dr.find_element_by_link_text ( ' set ' ) .click ()
dr.find_element_by_class_name ( " setpref " ) .click () 
the time.sleep ( . 1)    # this step must be added latency 
dr.find_element_by_link_text ( " Save Settings " ) .click ()
 # the time.sleep (. 1) 
test_alert = dr.switch_to .alert ()   # switch to Alert 
DEF alert_display ():
     the try : 
        T = test_alert.text
         Print (T)
         return True
     the except :
         return False
 IF alert_display (): 
    test_alert.accept () #Click OK to accept representation 
    test_alert.dismiss ()# Dismiss said they did not accept the pop-up box 
the else :
     Print ( " Alert does not eject " ) 

dr.quit ()

 

 

Difficulties:

  When initial use, we need to look at the built-in method

 

summarize:

  Require multiple operations, the establishment of a common operating Library Reference

Guess you like

Origin www.cnblogs.com/wuzhiming/p/11228584.html