JAVA-如何在硒中使用xpath [英] JAVA - How to use xpath in selenium
本文介绍了JAVA-如何在硒中使用xpath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个html代码:
i have this html code:
<select name="category" id="category">
<option value="0">«Seleziona la categoria»</option>
<option value='1' style='background-color:#ddd' disabled="disabled" id='cat1' >-- VEICOLI --</option>
<option value='2' id='cat2' >Auto</option>
</select>
,我必须选择由标签option
和文本Auto
标识的WebElement.
我尝试一些解决方案,例如:
and i have to select the WebElement identified by the tag option
with text Auto
.
I try some solution like:
d.findElement(By.xpath("/select[@id=category]/option[@id=cat2]")).click();
d.findElement(By.xpath("/select[@id=category]/option[text()='Auto']")).click();
d.findElement(By.xpath("//select[@id=category]/option[Auto]")).click();
但是每个人都给我
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"/select[@id=category]/option[@id=cat2]"} ( and other xpath i tried)
Command duration or timeout: 1.52 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
什么是正确的语法?有人可以帮我吗?
what is the right syntax? can someone help me?
推荐答案
您的XPath语法不正确.您需要在要匹配的文本属性值周围加上引号.试试:
You don't have your XPath syntax right. You need quotes round the text attribute values you're matching against. Try:
d.findElement(By.xpath("//select[@id='category']/option[@id='cat2']")).click();
这篇关于JAVA-如何在硒中使用xpath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文