Selenium
Selenium因为是使用浏览器驱动进行自动化测试,交互手段类似于前端手段,所以比较容易为前端所理解。
元素定位
xpath语法
元素信息
是否显示
is_displayed()
是否可用
is_enabled()
是否选中
is_selected()
元素标签名
tagname
元素大小、定位
rect
CSS属性
value_of_css_propety
children内容
text
props属性
get_attribute
用户操作行为
点击事件
element.click()方法
输入事件
注意到如果是input@type=file,可以触发文件上传功能
element.send_keys()方法
清空输入事件
element.clear()方法
表单提交
element.submit()
拖动事件
切换Frame
下载上传
Upload:
Because Selenium cannot interact with the file upload dialog, it provides a way to upload files without opening the dialog. If the element is an
inputelement with typefile, you can use the send keys method to send the full path to the file that will be uploaded.pythonimport os from selenium import webdriver from selenium.webdriver.common.by import By def test_uploads(driver): driver.get("https://the-internet.herokuapp.com/upload") upload_file = os.path.abspath( os.path.join(os.path.dirname(__file__), "..", "selenium-snapshot.png")) file_input = driver.find_element(By.CSS_SELECTOR, "input[type='file']") file_input.send_keys(upload_file) driver.find_element(By.ID, "file-submit").click() file_name_element = driver.find_element(By.ID, "uploaded-files") file_name = file_name_element.text assert file_name == "selenium-snapshot.png"
鼠标事件
浏览器交互
title
currentUrl
懒得写了

等待
https://www.selenium.dev/zh-cn/documentation/webdriver/waits/
强制等待
使用py提供的sleep语法
隐式等待
driver.implicitly_wait(2)隐式等待作用域driver,也就是全局,这会等待页面全部加载完成,在页面有乱七八糟的视频的时候代码执行效率不高,在页面比较庞大的时候效率也不高。
显示等待
wait = WebDriverWait(driver, timeout=2)
wait.until(lambda d : revealed.is_displayed())显式等待作用于某个元素,效率较高。而且且达成条件是一个确定的条件,比如元素存在但不可见,故一些场景更符合前端思维。
伪装身份
通过验证码
https://docs.capsolver.com/en/guide/extension/introductions/
获取网络请求
https://blog.csdn.net/weixin_45081575/article/details/126551260
https://blog.csdn.net/weixin_45081575/article/details/126389273
埋坑
Selenium交互章节,了解一下怎么搞的:
Additional validations
These methods are designed to closely emulate a user’s experience, so, unlike the Actions API, it attempts to perform two things before attempting the specified action.
- If it determines the element is outside the viewport, it scrolls the element into view, specifically it will align the bottom of the element with the bottom of the viewport.
- It ensures the element is interactable before taking the action. This could mean that the scrolling was unsuccessful, or that the element is not otherwise displayed. Determining if an element is displayed on a page was too difficult to define directly in the webdriver specification, so Selenium sends an execute command with a JavaScript atom that checks for things that would keep the element from being displayed. If it determines an element is not in the viewport, not displayed, not keyboard-interactable, or not pointer-interactable, it returns an element not interactable error
参考资料
https://bbs.huaweicloud.com/blogs/281997
https://blog.csdn.net/HRG520JN/article/details/125801099
https://www.w3school.com.cn/xpath/xpath_intro.asp
https://docs.capsolver.com/en/guide/extension/introductions/
