WEB自动化测试selenium

2021/6/19 23:26:47

本文主要是介绍WEB自动化测试selenium,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、Python+Selenium 环境搭建

python+Pycharm+selenium+chrome+谷歌浏览器驱动

二、八大元素定位

面试题:如果元素定位不到,你是怎么去分析的?

1.元素没有加载完成

2.Frame中(框架)

3.元素不可见,不可读,不可见

4.动态属性,动态的DIV层

前提:需要定位的元素或它的属性必须要唯一

ID、NAME、class_name、xpath、css、tag_name、link_text、partail_link_text

①、id定位:

driver.find_element(By.ID,"kw").send_keys("魔兽世界")

②、name定位:

driver.find_element(By.NAME,"wd").send_keys("魔兽世界")

③、link_text定位

driver.find_element(By.LINK_TEXT,"新闻").click()

④、partail_link_text定位

driver.find_element(By.PARTIAL_LINK_TEXT,"新").click()

⑤、xpath定位

相对路径:/开头的是绝对路径

/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input

举例:driver.find_element(By.XPATH,"/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/input").send_keys("魔兽世界")

相对路径://开头的相对路径  (ctrl+F)

//input

1.相对路径+索引定位:

//form/span[1]/input

2.相对路径+属性定位

//input[@autocomplete='off']

3.相对路径+通配符定位   *

//*[@autocomplete='off']   或//*[@*='off']

经常容易报错

4.相对路径+部分属性值定位

//input[starts-with(@autocomplete,'of')]  #以什么开头  或者    //*[starts-with(@autocomplete,'of')] 

//*[substring(@autocomplete,2)='ff']       # 以什么结尾

//*[contains(@autocomplete,'of')]       #包含

5.相对路径+文本定位

//span[text()='按图片搜索']

举例:

value = driver.find_element(By.XPATH,"//span[text()='按图片搜索']").get_attribute('class') print(value)

 

CSS定位:

1.绝对路径:不用

2.通过ID和CLASS 

 



这篇关于WEB自动化测试selenium的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程