Selenium automated testing practice 2-WebDriver advanced application

Advanced use of Selenium WebDriver

selenium waiting mechanism

Page-level wait mechanism: Define the timeout period for Selenium to wait for the page to load.
Use this function to set the wait time:driver.set_page_load_timeout(最长等待秒数)


Element-level waiting: commonly used in modern pages, the ajax request returns the waiting time of the entire process

  • Forced waiting: direct time.sleep(3) forced sleep
  • Implicit wait: use this code to set a grace time:webdriver.implicitly_wait(等待秒数)
  • Explicit wait (most recommended):WebDriverWait(WebDriver实例, 超时秒数, 检测时间间隔[可选], 可忽略异常集合[可选] )

Script-level waiting mechanism: set asynchronous script timeout
driver.set_script_timeout(最长等待秒数)


Keyboard and mouse simulation

All WebElement elements involved in the operation chain must exist at the same time and be in an operable state when the operation chain is executed

ActionChains operation chain, using the form of flow to process simulation events sequentially

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains # 记住导包

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

# ActionChains接收一个webdriver对象作为参数
# 链式调用的形式模拟点击
ActionChains(driver).click(driver.find_element(By.LINK_TEXT, "登录")).perform()

Complex operation chain simulation: drag and drop operation

<html>
	<head>
		<style type="text/css">
			#div1 {
      
      
				width: 300px;
				height: 100px;
				padding: 10px;
				border: 1px solid #aaaaaa;
			}
		</style>
		<script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
		<script src="http://apps.bdimg.com/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
		<script type="text/javascript">
			$(function () {
      
      
				$("#drag1").draggable();
				$("#div1").droppable({
      
      
					drop: function (event, ui) {
      
      
						alert("图片放置成功");
					},
				});
			});
		</script>
	</head>

	<body>
		<p>请把图片拖放到方框中:</p>
		<div id="div1"></div>
		<br />
		<img
			id="drag1"
			src="https://cdn.ptpress.cn/pubcloud/3/app/0718A6B0/cover/20191204BD
54009A.png"
		/>
	</body>
</html>
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
driver.get("D:\\TestDragAndDrop.html")

ActionChains(driver)\
    .click_and_hold(driver.find_element(By.ID, "drag1"))\
    .move_to_element(driver.find_element(By.ID, "div1"))\
    .release()\
    .perform()

The realization of the combination key, actionchainsprecise control of the action of pressing and popping up the combination key

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

baiduSearchInput = driver.find_element(By.ID, "kw")
baiduSearchInput.send_keys("hello world")

ActionChains(driver)\
    .key_down(Keys.CONTROL)\
    .send_keys("a")\
    .key_up(Keys.CONTROL) \
    .pause(3)\
    .key_down(Keys.CONTROL) \
    .send_keys("x") \
    .key_up(Keys.CONTROL) \
    .pause(3) \
    .key_down(Keys.CONTROL) \
    .send_keys("v") \
    .key_up(Keys.CONTROL) \
    .pause(3) \
    .send_keys(Keys.BACKSPACE)\
    .pause(3)\
    .send_keys(Keys.ENTER)\
    .perform()

Operating Cookies

Two ways to get cookies:

driver.get_cookies()  #获取所有的cookie对象集合
driver.get_cookie(cookie名称)   #根据名称获取单个cookie

driver.add_cookie(cookie对象) # 增加cookie
driver.delete_all_cookies()  #删除全部Cookie
driver.delete_cookie(cookie名称)  #按名称删除指定Cookie

browser startup parameters

There are many parameters that can be appended to the browser instance, this is the default value for all parameters

driver = webdriver.Chrome(executable_path='chromedriver', port=0, options=None,
service_args=None, desired_capabilities=None, service_log_path=None, chrome_options=
None, keep_alive=True)

JS executor for deep manipulation

webdriver.execute_script("JavaScript脚本", 自定义参数集(可选)) #执行同步脚本
webdriver.execute_async_script("JavaScript脚本", 自定义参数集(可选)) #执行异步脚本

In asynchronous scripts, JavaScript is processed asynchronously, but Selenium scripts are executed synchronously,
so there must be a callback callback function in asynchronous scripts, and selenium will always monitor this callback function to achieve smooth execution of asynchronous JS


Guess you like

Origin blog.csdn.net/delete_you/article/details/129998671