zl程序教程

您现在的位置是:首页 >  其他

当前栏目

Web自动化-Selenium自动化测试-5-复杂定位方式xpath CSS

定位测试Web自动化CSS 方式 selenium 复杂
2023-09-11 14:16:28 时间

浏览器常见操作

打开某个网页 http不可少
driver.get("url");
driver.navigate().to("url");

浏览器后退
driver.navigate().back();
浏览器前进
driver.navigate().forwars();

刷新
driver.navigate().refresh();
最大化
driver.manage().window().maximize();
设置窗口大小
driver.getCurrentUrl();
获取当前页Title
driver.getTitle();

简单进行元素定位 

//元素简单定位
driver.findElement(By.id("ID元素"));
driver.findElement(By.name("name属性"));
driver.findElement(By.className("classname属性")); //定位第一个元素
driver.findElements(By.className("class"));//返回一个list集合
driver.findElement(By.tagName("tag标签"));//span
driver.findElement(By.linkText("匹配文本值"));//精准匹配文本值
driver.findElement(By.partialLinkText("产品"));//部分匹配文本值

多元素定位:

多元素定位
List<WebElement >webElementList = driver.findElements(By.tagName("tagName"));

层级元素定位 

层级元素定位:
先定位父元素,然后再从父元素中精确定位出我们需要选取的子元素,层级定位一般的应用场景是无法直接定位到需要选取的元素,但是其父元素比较 容易定位,
通过定位父元素再遍历其子元素选择需要的目标元素,或者需要定位 某个元素下所有的子元素

xpath CSS对元素进行定位

 css 定位定位

// #main-page > div.header > nav > div > ul:nth-child(4) > li > form > div > input
driver.findElement(By.cssSelector("css取值"));

通过id
By.cssSelector("input #kw")

 

Xpath定位

相对路径  //   //*[@id="main-page"]/div[1]/nav/div/ul[2]/li/form/div/input  *匹配全部
 //input[@id="id互相"]

绝对路径  /  /html/body/div[2]/div[1]/nav/div/ul[2]/li/form/div/input
driver.findElement(By.xpath(" //input[@id=\"id互相\"]"));


通过classname定位
.s_ipt    定位class=s_ipt的元素
class=bg s_ipt_wr quickdelete-wrap,类似这种叫复合class,由多个类选择器组成,定位的写法则是:.bg.s_ipt_wr.quickdelete-wrap,所有空格用.(点)代替

and or 连接操作

//逻辑与或 and or 运算   //标签名[@属性名=值 and @属性名 = 值]
driver.findElement(By.xpath(" //input[@classname=\"class\"] and @name = '' "));
//层级定位 //div[@id=""]//a[@name=""]

文本定位

//文本定位  //标签名[text() = "" and @name =""]
driver.findElement(By.xpath(" //a[text() ='' and @name = ''   ] "));
//包含  //标签名[contains(text()/@属性,"被包含的内容")  and @name =""]
driver.findElement(By.xpath(" //a[contains(text(),\"百度\") ='' and @name = ''   ] "));
//table//tr//input[1] 和 //table//tr[1]//input[1]
前者是匹配table下的所有的tr下的第一个input节点 所有查询结果可能存在多个
后者是匹配table下的第一个tr结点的第一个input结点

 

last() 最后一个 表示Index的最后一个下标
//input[@name='identity' or @class='Volvo'][1]
//input[@name='identity' or @class='Volvo'][last()]
start-with()  
//div[starts-with(@id,'in')] ,表示选择以’in’开头的id属性的div节点
not() 表示否定
//input[@name=‘identity’ and not(contains(@class,‘a’))] 

元素属性定位
input[id=kw]
input[class=s_ipt]
input[id=kw][class=s_ipt]

模糊匹配
span[class ^=bg]    匹配所有span标签class属性值bg开头的元素
span[class $=rap]   匹配所有span标签class属性值rap结尾的元素
span[class *=quick] 匹配所有span标签class属性值中间有quick的元素

元素层级定位
1、子元素定位(>大于号)
(1)span>input    定位span标签下的input标签
(2)form>span>input    定位form标签下span标签下的input标签
以大于号>为分层,查找元素定位必须一层一层的以>往下写,有多个则匹配多个
2、后代元素(空格)
(1)span input   定位span下所有的input标签,包括span下的所有层次的input,注意不是span下一层的input,是所有
3、元素层级css还支持三个方法,分别是first-child、last-child、nth-child(n)
(1)first-child:第一个后代元素
(2)last-child:最后一个后代元素
(3)nth-child(N):指定第N个后代元素

 (1) input:first-child   定位所有层次第一个为input的元素,注意是第一个元素为input标签的 ()()
(2)span input:first-child    定位span标签下,第一个为input标签的元素
(3)span :last-child   定位span标签下,最后一个元素
(4)span input:last-child    定位span标签下,最后一个为input标签的元素
(5)span :nth-child(2)  定位span标签下,第二个元素
(6)form.fm>:nth-child(2)  定位form标签,class等于fm下的第二个元素

 

执行JS操作

 String js = "var user_input = document.getElementById(\"passport_51_user\").title;return user_input;";
 String input_title=(String)((JavascriptExecutor)driver).executeScript(js);