Selenium Common Operations for Web Automation

This article summarizes some common operations that will be used when using selenium for web/UI automation.

positioning element

driver.find_element_by_xpath()#1, absolute path 2, element attribute 3, combination of hierarchy and attribute 4, use logical operator 
driver.find_element_by_id()#according to id positioning, HTML stipulates that the id attribute must be the only 
driver in the HTML document. find_element_by_name()#name attribute positioning 
driver.find_element_by_class_name()#class attribute positioning 
driver.find_element_by_tag_name()#tag name positioning Because the tags are basically not unique, so don’t use this 
driver.find_element_by_link_text()#Locate the text link, you need the corresponding element Has href attribute 
driver.find_element_by_partial_link_text()#Fuzzy positioning text link 
driver.find_element_by_css_selector()#CSS selector

By positioning elements

It is another way of element positioning, which is the same as the above 8 bottom layers.

from selenium.webdriver.common import By
driver.find_element(By.ID,'kw')

locate a set of elements

driver.find_elements_by_xpath()#Other methods are the same
driver.find_elements(By.XPATH,'') 

browser operation

driver.set_window_size()#Set the size of the browser, which can be set to the size of the mobile terminal 
driver.back()#Browser back 
driver.forward()#Browser forward 
driver.set_window_position() 
driver.set_window_rect() 
driver.refresh() #Simulate browser refresh 
driver.maximize_window()#Maximize browser

Set browser parameters

Set the chrome_options parameter when defining the driver, which is an object instantiated by the Options class. Among them, the commonly used parameters are to set whether the browser is visualized and the browser's request header information. The former can speed up the running speed of the code, and the latter can effectively prevent the anti-crawler detection of the website.

from selenium.webdriver.chrome.options import Options
url='https://movie.douban.com/'
#Options类实例化
chrome_options=Options()
#设置浏览器参数
#--headless是不 显示浏览器启动和执行过程c
chrome_options.add_argument('--headless')
#设置lang和User-Agent信息,防止反爬虫检测
chrome_options.add_argument('lang=zh_CN.UTF-8')
UserAgent='Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.5112.101 Safari/537.36'
chrome_options.add_argument('User-Agent='+UserAgent)
#启动浏览器并设置chrome_options参数

driver=webdriver.Chrome(options=chrome_options)
driver.get(url)
driver.maximize_window()
print(driver.title)
# print(driver.page_source) #获取网页的html代码

element manipulation

driver.find_element_by_xpath().click()#click element 
driver.find_element_by_xpath().clear()#clear text 
driver.find_element_by_xpath().send_keys()#simulate keyboard input

 page interaction

The webElement interface provides some methods that can interact with the page

submit and click are interchangeable in some cases, and submit is only used for form submit buttons.

driver.find_element_by_xpath().submit()#Submit the content of the input box is similar to press Enter to submit the content of the search box 
driver.find_element_by_xpath().size#Return element size 
driver.find_element_by_xpath().text#Get element text 
driver.find_element_by_xpath() .get_attribute()#Get element attribute value 
driver.find_element_by_xpath().is_selected()#Whether it is selected 
driver.find_element_by_xpath().is_enabled()#Judge whether the element can be used 
driver.find_element_by_xpath().is_displayed()#Return element whether user visible

mouse event

Methods about mouse operations are encapsulated in Actionchains

from selenium.webdriver.common.action_chains import ActionChains 
rigt_check=driver.find_element_by_id('kw') 
ActionChains(driver).context_click(rigt_check).perform()#context_click performs right-click operation on elements# 
perform()Execute all actions stored in ActionChains Behavior 
ActionChains(driver).move_to_element(rigt_check).perform()#Mouse over 
ActionChains(driver).double_click(rigt_check).perform()#Mouse double click 
ActionChains(driver).drag_and_drop(source,target).perform()# The mouse is dragged from the source element to the target element

url='https://passport.bilibili.com/login'

driver=webdriver.Chrome()
driver.get(url)
#双击登录按钮
ele=driver.find_element_by_class_name('tit')
ActionChains(driver).double_click(ele).perform()
sleep(2)
#拖拽滑块
ele=driver.find_element_by_class_name('gt_slider_knob,gt_show')
ActionChains(driver).drag_and_drop_by_offset(ele,100,0).perform()

keyboard events

Simulate keyboard input, which can be a key or a combination of keys.

from selenium.webdriver.common.keys import Keys 
driver.find_element_by_xpath().send_keys(Keys.BACK_SPACE)#Operate the element once backspace 
driver.find_element_by_xpath().send_keys(Keys.SPACE)#Enter spaces for the element 
driver.find_element_by_xpath ().send_keys(Keys.CONTROL,'a')#ctrl+A select all input box content 
driver.find_element_by_xpath().send_keys(Keys.CONTROL,'x')#ctrl+x cut input box content 
driver.find_element_by_xpath ().send_keys(Keys.CONTROL,'v')#ctrl+v paste the content into the input box 
driver.find_element_by_xpath().send_keys(Keys.ENTER)#Replace the click operation with the Enter key 
driver.find_element_by_xpath().send_keys (Keys.F1)#Keyboard F1

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from time import sleep
url='https://www.baidu.com'

driver=webdriver.Chrome()
driver.get(url)
ele=driver.find_element_by_id('kw')
ele.send_keys("selenium")
sleep(2)
#删除最后一个文字
ele.send_keys(Keys.BACK_SPACE)
sleep(2)

#添加空格键+教程
ele.send_keys(Keys.SPACE)
ele.send_keys("教程")
sleep(2)

#ctrl+a全选输入框内容
ele.send_keys(Keys.CONTROL,'a')
sleep(2)


#ctrl+x 剪切输入框内容
ele.send_keys(Keys.CONTROL,'x')
sleep(2)


#ctrl+v 粘贴内容到输入框
ele.send_keys(Keys.CONTROL,'v')
sleep(2)

#回车键代替单击
driver.find_element_by_id('su').send_keys(Keys.ENTER)

Get verification information

After the execution of the automation use case is completed, some information can be obtained from the page to prove the success or failure of the use case execution

driver.title #Get page title 
driver.current_url#Get page curl 
driver.find_element_by_xpath().text#Get element text

 element wait

Since most web pages use AJAX technology, when the browser loads the page, the elements on the page may not be loaded at the same time, and need to wait.

Explicit waiting: Explicit waiting can wait flexibly according to the judgment conditions. The program detects it every once in a while. If the detection result matches the condition, it will execute the next step. Otherwise, it will continue to wait until the maximum time set is exceeded.

from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
element=WebDriverWait(driver,5,0.5).until(EC.presence_of_element_located(By.ID ,'kw')) 
#Check whether this element can be located every 0.5 seconds, and timeout after 5 seconds 
#expected_conditions provides many expected condition judgment methods 
#You can also use is_displayed() to judge whether the element is visible

implicit wait

Implicit waiting refers to detecting whether the webpage is loaded within a set period of time, that is, under normal circumstances, the user will not execute the next step until the small circle in the browser tab bar does not turn. For example, if the waiting time is set to 30 seconds in the code, as long as the webpage finishes loading within 30 seconds, it will automatically execute the next step, and if it exceeds 30 seconds, an exception will be thrown. It is worth noting that implicit waiting works on the entire driver cycle, so it only needs to be set once.

Wait for a certain period of time to complete loading of an element on the page. If the element has not been loaded beyond the set period of time, an exception of no element will be thrown.

Usually set behind the driver.

driver.implicitly_wait(10)

sleep sleep method

Add where needed.

from time import sleep
sleep(3)

Implicit waiting and explicit waiting are more flexible and intelligent than the mandatory waiting of time.sleep, which can solve various network delay problems. Implicit waiting and explicit waiting can be used at the same time, but the longest waiting time depends on The maximum number between the two, if the implicit waiting time of the above code is 30 seconds, and the explicit waiting time is 20 seconds, then the longest waiting time of the code is the implicit waiting time. 

Multi-frame switching

Frame is a frame page, which is no longer supported in HTML 5, but it can still be seen in some websites. The function of the frame is to nest one or more different HTML codes in the HTML code, and each nested HTML code needs to be realized by the frame

The functions implemented by iframe and frame are the same, but the usage and flexibility are different. Whether it is an iframe or a frame, the positioning and operation of Selenium are the same

Since one HTML can nest one or more iframes, Selenium needs to switch to the specified iframe through switch_to.frame() when operating different iframes, and then perform the corresponding operation 

driver.switch_to.frame('kw')#You can directly take the id or name attribute of the form. If the form does not have these two attributes, you can first locate the form element, and then switch 
driver.switch_to.parent_frame('kw')#Jump out of the current First-level form 
driver.switch_to.default_content('kw')#Jump back to the outermost page 
#Locate according to the index 
driver.switch_to_frame(0) 
#According to the id or name attribute 
driver.switch_to_frame('framea') 
new version driver.switch_to. frame(0)

Such as the text box that Baidu knows 

The following code realizes opening the Baidu Knowing topic page, click I want to answer, navigate to the iframe, enter the content, jump out of the iframe, and click the submit answer button

url='https://zhidao.baidu.com/question/1903337615241287780.html'

driver=webdriver.Chrome()
driver.get(url)
driver.maximize_window()
#点击我要回答
driver.find_element_by_id('answer-bar').click()
#切换到frame内部的html
driver.switch_to.frame('ueditor_0')
#定位frame内部的元素
driver.find_element_by_xpath('/html/body').send_keys('美国')
#跳回网页HTML 如果不切回网页,则找不到提交回答按钮,且js代码也执行不了
# driver.switch_to.default_content()
js='window.scrollTo(0,100)'
driver.execute_script(js)
sleep(3)
#点击网页提交回答按钮
driver.find_element_by_xpath("//div[@class='addons line']/a").click()

 Multi-window switching

search_windows=driver.current_window_handle #Get the current window handle
all_handles=driver.window_handles #Get handles of all windows
driver.switch_to.window(handle)#switch window
handles=driver.window_handles
driver.switch_to_window(handles[0])
driver.switch_to_window(handles[1])

application

from selenium import webdriver
from selenium.webdriver.common.by import By
url='https://www.baidu.com/'
driver=webdriver.Chrome()
driver.get(url)
driver.implicitly_wait(10)
search_windows=driver.current_window_handle #获得窗口句柄
driver.find_element_by_link_text('登录').click()
driver.find_element_by_link_text('立即注册').click()
import time

#获得所有窗口的句柄
all_handles=driver.window_handles

#切换到注册窗口
for handle in all_handles:
    if handle!=search_windows:
        driver.switch_to.window(handle)
        print("当前是注册窗口")
        time.sleep(2)

#回到搜索窗口
for handle in all_handles:
    if handle==search_windows:
        driver.switch_to.window(handle)
        print("当前是搜索窗口")
        driver.find_element_by_id('TANGRAM__PSP_4__closeBtn').click()#关闭注册窗口
        driver.find_element_by_id('kw').send_keys("selenium")
        driver.find_element_by_id('su').click()
        time.sleep(2)
driver.quit()



Alert Box Handling

It is very simple to process alert, confirm, and prompt generated by JavaScript in webdriver. The specific method is to use the switch_to_alert method to locate alert/confirm/prompt, and then use text/accept/dismiss/send_keys and other methods to operate.

text: returns the text information in alert/confirm/prompt

accept(): accept the existing alert box

dismiss(): dismiss the existing warning box

send_keys: Send text to the alert box

Apply, switch to alert as shown in the figure and accept

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from time import sleep
url='https://www.baidu.com/'
driver=webdriver.Chrome()
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)
#鼠标悬停至设置链接
link=driver.find_element_by_id("s-usersetting-top")
ActionChains(driver).move_to_element(link).perform()

driver.find_element_by_link_text('搜索设置').click()

#保存设置
driver.find_element_by_class_name('prefpanelgo').click()
sleep(2)
#接受警告框
# driver.switch_to_alert().accept()

driver.quit()

 upload files

The general file upload is to open the windows window and select the local file from the window, but the webdriver cannot operate the windows control.

1. Ordinary uploads pass the local file path as a value to the input tag. Note that it must be an input tag

2. Plug-in upload, upload based on flash, js, Ajax and other technologies

#Locate the upload button, add a local file 
driver.find_element_by_name('file').send_keys("D:\\upload_file.txt")

Another method is to implement uploading based on AutoIt software. But it is not recommended because it is not in the scope of Python.

download file

webdriver allows setting the default file download path, and the file will be automatically downloaded and stored in the set directory

operating cookies

Under what circumstances is the cookie operation used? When a developer develops a function, when the user logs in, the user's username will be written into the browser cookie, and the key is specified as username, then the username can be found through get_cookies() and the value will be printed.

url='https://www.youdao.com/'

driver=webdriver.Chrome()
driver.get(url)
driver.maximize_window()
driver.implicitly_wait(10)

# #获得cookie

driver.add_cookie({'name':'Login_User','value':'Password'})
cookie=driver.get_cookies()
print("所有Cookie为",cookie)
#获取name为Login_User的cookie
one_cookie=driver.get_cookie('Login_User')
print("Login_User的Cookie为",one_cookie)
# "删除name为Login_User的cookie
driver.delete_cookie('Login_User')

surplus_cookies=driver.get_cookies()
print("剩余的cookie为:",surplus_cookies)

driver.delete_all_cookies()
surplus_cookies=driver.get_cookies()
print("剩余的cookie为:",surplus_cookies)
driver.quit()

The return result is

所有Cookie为[{
	'domain': 'www.youdao.com',
	'httpOnly': False,
	'name': '___rl__test__cookies',
	'path': '/',
	'secure': False,
	'value': '1662293521877'
}, {
	'domain': '.youdao.com',
	'expiry': 1696853521,
	'httpOnly': False,
	'name': 'OUTFOX_SEARCH_USER_ID',
	'path': '/',
	'secure': False,
	'value': '"[email protected]"'
}, {
	'domain': 'www.youdao.com',
	'httpOnly': False,
	'name': 'Login_User',
	'path': '/',
	'secure': True,
	'value': 'Password'
}, {
	'domain': '.youdao.com',
	'expiry': 1696853520,
	'httpOnly': False,
	'name': 'OUTFOX_SEARCH_USER_ID_NCOO',
	'path': '/',
	'secure': False,
	'value': '1883563674.2397'
}]
Login_User的Cookie为 {
	'domain': 'www.youdao.com',
	'httpOnly': False,
	'name': 'Login_User',
	'path': '/',
	'secure': True,
	'value': 'Password'
}
剩余的cookie为: [{
	'domain': 'www.youdao.com',
	'httpOnly': False,
	'name': '___rl__test__cookies',
	'path': '/',
	'secure': False,
	'value': '1662293521877'
}, {
	'domain': '.youdao.com',
	'expiry': 1696853521,
	'httpOnly': False,
	'name': 'OUTFOX_SEARCH_USER_ID',
	'path': '/',
	'secure': False,
	'value': '"[email protected]"'
}, {
	'domain': '.youdao.com',
	'expiry': 1696853520,
	'httpOnly': False,
	'name': 'OUTFOX_SEARCH_USER_ID_NCOO',
	'path': '/',
	'secure': False,
	'value': '1883563674.2397'
}]
剩余的cookie为: []

Call Javascript

Can be used to control the browser scroll bar

url='https://www.baidu.com/'

driver=webdriver.Chrome()
driver.get(url)

driver.find_element_by_id('kw').send_keys("selenium")
driver.find_element_by_id('su').click()
driver.set_window_size(600,600)
#通过js设置浏览器窗口的滚动条位置
js="window.scrollTo(500,500);"
driver.execute_script(js)

 window screenshot

url='https://www.baidu.com/'

driver=webdriver.Chrome()
driver.get(url)

driver.find_element_by_id('kw').send_keys("selenium")
driver.find_element_by_id('su').click()
sleep(2)
driver.get_screenshot_as_file("C:\\Users\\yangyl13\\Desktop\\3.png")
driver.quit()

close the window

driver.quit() #Exit the relevant driver, close all windows

driver.close()#Close a single window.

Verification code processing

1. Remove the verification code from the test environment

2. Universal verification code

3. Verification code recognition technology such as python-tesseract

4. Record cookies

drop down box selection

from selenium.webdriver.support.select import Select 
Select(driver.find_element_by_id('')).select_by_index('2')#Select according to the drop-down box index Select 
(driver.find_element_by_id('')).select_by_value('Python') #Select according to the value attribute of the drop-down box 
Select(driver.find_element_by_id('')).select_by_visible_text('Python')#Select according to the visible text of the drop-down box

Guess you like

Origin blog.csdn.net/seanyang_/article/details/126685726