selenium之xpath /css

本文详细介绍XPath与CSS选择器的使用方法,包括如何通过属性、文本内容定位页面元素及组合条件筛选。适用于Web自动化测试与爬虫开发。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

xpath:

a'a   @title=\"a'a\

//a[contains(text(),'product_08')]/ancestor::tr//input[@class='live-tv-overview-delete']

 

WebElement delConfirmBut = driver.findElement(By.xpath("//div[starts-with(@id,'confirmDialog')]/div/div/div[3]/button[1]"));
delConfirmBut.click();

Actions action = new Actions(driver);
action.moveByOffset(4, 5).click();

 

1.last()-n position()<n
2.contains,startwith,endwith and or
3. /B[1] [Last()]第一个B的子元素
4. //@*  任意属
 
form/element[last()]
form/element[last()-n] 选择倒数第几个
form/element[position()<n]位置小于n的element
form/element[@attr_name>Number] form节点下所有带attr_name属性,且属性值大于number的element
form/element[@attr_name<Number]
path1|path2  匹配1和2
//form/a[contains(text(),'text')]
xpath=//tbody[starts-with(@id,'normalthread_')]//td[@class='by'][1]//em//span[contains(@title,'${today}')][1]/parent::tr 
 
WebElement hello = driver.findElement(By.id("div1")).findElement(By.lindText("hello"));
=driver.findElement(By.tagName("").tagName(""));
 
 
(By.xpath("//em[ @testid='123'   and/or   @id='button_delete' ]")).click();
(By.xpath("//em[ @testid='123'   and contains(text(),'nash') ]"))
( B y. x p a t h ("//input[@type='submit'][@value='Login']")); 
(B y .x p a t h ("img[@alt]")); 
wd.findElement(By.xpath("//a[text()='商户管理']")).click();
 
 //div[contains(@id,'mzLoginArea4')]
(B y .x p a t h( " / / t d [c o n t ai n s ( te x t ( ), ' N a sh ' )]
//div[a[@id= 'mzLogin']]
 
 

driver.findElement(By.xpath("//a[contains(text(),'aaa')]"));

driver.findElement(By.xpath("//a[text()='aaa']"));

driver.findElement(By.xpath(".//*[@id='buy_count']/../a[contains(.,'+')]"));

                   <input id="buy_count" type="text" disabled="disabled" readonly="readonly" value="1">

          <a class="plus_proinfo" href="javascript:;">+</a>

                  

driver.findElement(By.xpath("//a[contains(@id,'aaa')][1]"));

driver.findElement(By.xpath("//a[@id='123'][@value='sadcds']"));

                   //body[form]     body/a

                  

driver.findElement(By.xpath("//a[starts-with(@id,'aaa_')]"));

driver.findElement(By.xpath("//a[starts-with(@id,'aaa_') and contains(text(),'sds')]"));

driver.findElement(By.xpath("//a[ends-with(@id,'aaa_') or contains(text(),'sds')]"));

                  

driver.findElement(By.xpath("//a/div[Last()-1]"));

driver.findElement(By.xpath("//a/div[@classnum<5]"));

                  

driver.findElement(By.tagName("a").id("cdcd"));

driver.findElement(By.tagName("a")).findElement(By.id("sd"));

                  

如果要继续找子节点呢?we.findElement(By.xpath("//p"),但是,这样写是不对的,其结果是找到全部p标签,正确的写法是  we.findElement(By.xpath(".//p"),表示当前WebElement下的p标签。

 

Css:

 
1.driver.findElement(By.cssSelector("#username"));  
2.driver.findElement(By.cssSelector("标签名[属性名='属性值']"));  
3.
^=  driver.findElement(By.cssSelector("标签名[属性名^='xxx']"));  匹配属性值以xxx开头的元素
$=  driver.findElement(By.cssSelector("标签名[属性名$='xxx']"));  匹配属性值以xxx结尾的元素  
*=  driver.findElement(By.cssSelector("标签名[属性名^='xxx']"));  匹配属性值包含xxx的元素 

 


转载于:https://www.cnblogs.com/season-xie/p/5330813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值