查找多个不同的XPath元素。

huangapple go评论87阅读模式
英文:

findElements by multiple different xpaths

问题

我有这段代码,我不确定为什么它不起作用。

String xpath1 = "//span[contains(text(),'我是 span')]";
String xpath2 = "//div[contains(text(),'我是 Div')]";
String xpath3 = "//input[contains(text(),'我是 input')]";

然后我将所有的 xpath 组合起来,像这样。

String majorxpath = "\"" + xpath1 + " or " + xpath2 + " or " + xpath3 + "\"";
List<Webelement> results = driver.findelements(By.xpath(majorxpath));

这会返回错误,无法通过 majorxpath 找到元素。

请注意,我想一起评估 xpath1、xpath2 和 xpath3,如果其中任何一个存在,就应该将 web 元素返回在 results 中。

另外,请注意,这些 xpath 单独运行是有效的,但我想查看它们是否存在,并返回 web 元素。

英文:

I have this bit of code, I am not sure why it is not working.

String xpath1=&quot;//span[contains(text(),&#39;I am span&#39;)]&quot;;
String xpath2=&quot;//div[contains(text(),&#39;I am Div&#39;)]&quot;;
String xpath3=&quot;//input[contains(text(),&#39;I am input&#39;)]&quot;;

then I combnie all the xpaths like this.

String majorxpath=&quot;\&quot;&quot;+xpath1+&quot; or &quot;+xpath2+&quot; or &quot;+xpath3+&quot;\&quot;&quot;;
List&lt;Webelemnt&gt; results=driver.findelements(By.xpath(majorxpath));

This returns error, unable to locate an elemnent by majorxpath.

Please note that, I want to evaluate xpath1, xpath2, xpath 3 together and if any of them exists, the webelemnt should return in results

Also, note the xpaths work individually but I want to see if any of them are existing and return the webElemnt.

答案1

得分: 2

尝试以下 xpath 进行识别。

//span[contains(text(),'我是 span')] | //div[contains(text(),'我是 Div')] | //input[contains(text(),'我是 input')]
英文:

Try below xpath to identify.

//span[contains(text(),&#39;I am span&#39;)] | //div[contains(text(),&#39;I am Div&#39;)] | //input[contains(text(),&#39;I am input&#39;)]

huangapple
  • 本文由 发表于 2020年10月7日 23:34:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64247433.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定