简体   繁体   English

知道为什么这个xpath无法正常工作吗?

[英]Any idea why this xpath isn't working?

Here's is the html elements and the command I'm running which is checking if "lol" exists on the page. 这是html元素和我正在运行的命令,用于检查页面上是否存在“大声笑”。 It's not finding "lol" and not sure why. 它找不到“大声笑”,也不知道为什么。

<svg data-reactid=".0.1.0.0.1.0" height="2129" width="1252">
  <g data-reactid=".0.1.0.0.1.0.$http=2//10=1203=167=127=29090/img/103?token=0a11944b491366ab96caf34c8e3991d436c86d655">
    <image height="2129px" width="1252px"xlink:href="http://10.203.67.27:9090/img/103?token=a11944b491366ab96caf34c8e3991d436c86d655">
  </g>
  <g data-reactid=".0.1.0.0.1.0.1">
     <line class="severity-inactive " data-reactid=".0.1.0.0.1.0.1.$conn2" y2="1209.895121951221" x2="677.0326086956554" y1="708.8012195121894" x1="1126.1195652173908" style="stroke-width: 7;">
     <line class="severity-inactive " data-reactid=".0.1.0.0.1.0.1.$conn4" y2="329.7353658536632" x2="841.593360995849" y1="708.8012195121894" x1="1126.1195652173908" style="stroke-width: 7;">
  </g>
  <g data-reactid=".0.1.0.0.1.0.2">
    <g data-reactid=".0.1.0.0.1.0.2.$node4">
      <circle class="severity3 " data-reactid=".0.1.0.0.1.0.2.$node4.0" r="9.674435999999993" cy="708.8012195121894" cx="1126.1195652173908">
      <g data-reactid=".0.1.0.0.1.0.2.$node4.1">
         <rect class="node-label-background"  data-reactid=".0.1.0.0.1.0.2.$node4.1.0" height="21" width="22" y="716.8012195121894" x="1115.1195652173908">
         <text class="node-label" data-reactid=".0.1.0.0.1.0.2.$node4.1.1" y="721.3012195121894" x="1118.1195652173908">lol</text>
    </g>
 </g>


 By.xpath(//*[name()='svg']/*[name()='text' and text()='"+lol+"'])

This is correct xpath: 这是正确的xpath:

By.xpath("//*[name()='svg']//*[name()='text' and text()='lol']")

Add quotes, use 'lol' string (not variable) and add second slash before text element locator (this is not svg's direct child). 添加引号,使用“ lol”字符串(不是变量),并在text元素定位符之前添加第二个斜杠(这不是svg的直接子级)。

I think you are missing a '/' 我认为您缺少'/'

Yours: By.xpath(//*[name()='svg']/*[name()='text' and text()='"+lol+"']) 您的: By.xpath(//*[name()='svg']/*[name()='text' and text()='"+lol+"'])

Mine : By.xpath(//*[name()='svg']//*[name()='text' and text()='"+lol+"']) 我的: By.xpath(//*[name()='svg']//*[name()='text' and text()='"+lol+"'])

How about //svg//text[contains(text(),'lol')] 怎么样//svg//text[contains(text(),'lol')]

(selecting //* for the sake of one tag, whose name is known to you ( svg ) is quite inefficient, selecting it directly is much better; similarly in case of text - you already know the name of tag, so just use it; finally contains takes care of the fact that searched text may only be part of the entire text) (为了一个标签而选择//* ,这个标签的名字是您不知道的( svg )效率很低,直接选择它要好得多;类似的是在text情况下-您已经知道标签的名称,因此只需使用它;最后contains一个事实,即搜索到的文本可能只是整个文本的一部分)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM