我有两个链接
订婚戒指
戒指
上面的链接是下拉菜单下的复选框
如何使用text()
方法使用Selenium xpath选择Rings
以下是我尝试过的方法,但它选择了“订婚戒指”
//li//a[contains(text(),'Rings')]
但它正在选择“订婚戒指”,因为它出现在菜单中的“铃声”之前
我的代码:
//Select Sub-Menu item from collection dropdown
WebElement selectSub = driver.findElement(By.xpath("//div[@class='popover fade right in']//div[2]//li//a[contains(text(),'Rings')]"));
最佳答案
之所以选择Engagement Rings
,是因为您使用的是contains()
方法
它将找到包含匹配文本的元素,因此您的第一个复选框为“环”文本
只需将xpath更改为:
//li//a[text()='Rings']
要么
//li//a[normalize-space()='Rings']
关于selenium - 如果两个链接中包含相同的文本,如何选择绝对链接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44243269/