[英]How to locate element using css selector in selenium webdriver java?
I am trying to locate the text of the third div role:我试图找到第三个 div 角色的文本:
<div class="jss295">
<div class="section-header">
<h3 class="section-header-title">LABELS</h3></div>
<div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">system-all</span></div>
<div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">paloaltonetworks_iPoiycgPut</span></div>
<div role="button" class="jss300 jss299" tabindex="-1"><span class="jss313">system-paloaltonetworks</span></div>
</div>
I tried xpath
as follows: driver.findElement(By.xpath("//span[contains(text(), '" + str + "')]")).getText()
, where str
is the text I need.我试过
xpath
如下: driver.findElement(By.xpath("//span[contains(text(), '" + str + "')]")).getText()
,其中str
是我需要的文本。
xpath
works but I'd like to use cssSelector
instead. xpath
有效,但我想改用cssSelector
。
:contains() CSS pseudo-class selector is removed from the CSS3 spec.If you are using the latest browsers, this will not work which confronts to new CSS standards. :contains() CSS 伪类选择器已从 CSS3 规范中删除。如果您使用的是最新的浏览器,这将无法工作,这会遇到新的 CSS 标准。
If the className is unique you can use cssSelector as:如果 className 是唯一的,您可以将 cssSelector 用作:
span.valueOfClass
//It seems all classNames are same in your scenario. //在您的场景中,所有类名似乎都相同。
I guess it is better to use XPath.我想最好使用 XPath。
Just give a try:试试看:
By.cssSelector(span[text()$=paloaltonetworks])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.