css定位表达式:https://www.w3schools.com/cssref/css_selectors.asp
XPath定位: https://www.w3schools.com/xml/xpath_syntax.asp
PO参考文档:
- selenium python库中的文档 https://selenium-python.readthedocs.io/page-objects.html
- firefox母公司开源 PyPOM https://github.com/mozilla/PyPOM/tree/master/src/pypom
淘宝镜像下载webdriver地址:https://npm.taobao.org/mirrors/chromedriver
Appium和Selenium对比
- Appium == Selenium Server
- ChromeDriver == ChromeDriver
- Appium Client <= Selenium Client
- Appium Desktop == Selenium IDE
- Selenium Grid 支持所有的webdriver的框架 appium selenium
传统和远程调用webdriver区别
'''传统模式下:调用过程 python testcase -> chromedriver -> chrome'''# 用法webdriver.Chrome()'''Remote模式下:调用过程 python testcase -> 远程selenium server -> chromedriver/iedriver -> chrome/ie'''# 需要先在终端运行服务器jar包java -jar selenium-server-standalone-3.141.59.jar -help # 查看帮助文档java -jar selenium-server-standalone-3.141.59.jar -debug # 以deug方式运行# 用法self.driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)'''Grid模式下调用过程python testcase -> grid hub/类似STF -> grid node == selenium server -> chromedriver/iedriver -> chrome/ie'''
常用定位方法:
# xpath定位self.driver.find_element_by_xpath('//*[contains(text(),"xxxx")]')self.driver.find_element_by_xpath('//*[text()[contains(.,"目录")]]')# css定位self.driver.find_element_by_css_selector('.toc-container .btn')self.driver.find_element_by_css_selector('.btn.btn-default[data-toggle=dropdown]')
定位元素如下图:
前端控制台查找元素技巧:
# xpath定位$x('//*[@data-toggle="dropdown" and @class="btn btn-default"]')$x('//*[text()[contains(., "目录")]]')# css定位$('.toc-container .btn.btn-default') # 两个类中间有空格,为父子级关系$('.btn.btn-default[data-toggle=dropdown]')# 两个类挨着为同级(一个元素有两个或多个类名)# js定位document.getElementByIddocument.getElementsByXXX
