Selenium: 有没有办法找到一个内部没有内容的元素?

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

Selenium: is there any way to find an element that has nothing inside?

问题

I'd like to find an element that has nothing inside.
For example:

<div class="top">
 selection: 
 <b></b>
</div>

It displays like this:

selection:

In this HTML code, <b></b> has nothing inside. But for testing purposes, I want to find it without text inside.
I tried
findElement(By.xpath("//div[contains(@class,'top')]/b[not(node())]")
but it didn't return anything.
Is there a way to locate <b></b>?

英文:

I'd like to find an element that has nothing inside.
For example:

&lt;div class=&quot;top&quot;&gt;
 selection: 
 &lt;b&gt;&lt;/b&gt;
&lt;/div&gt;

It displays like this:

selection:

In this HTML code, &lt;b&gt;&lt;/b&gt; has nothing inside. But for testing purpose, I want to find it without text inside.
I tried
findElement(By.xpath(&quot;//div[contains(@class,&#39;top&#39;)]/b[text()==&#39;&#39;]&quot;)
but it didn't return anything.
Is there a way to locate &lt;b&gt;&lt;/b&gt;?

答案1

得分: 1

Remove the text() matcher.

//div[contains(@class,'top')]/b

Will match the <b>

Alternatively:

//div[contains(@class,'top')]/b[not(text())]
英文:

Remove the text() matcher.

//div[contains(@class,&#39;top&#39;)]/b

Will match the &lt;b&gt;

Alternatively:

//div[contains(@class,&#39;top&#39;)]/b[not(text())]

huangapple
  • 本文由 发表于 2023年5月18日 01:04:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76274559.html
匿名

发表评论

匿名网友

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

确定