简体   繁体   English

"如何在xpath中访问类下的第二个span标签"

[英]how to access second span tag under class in xpath

I am trying to access data from XPath for the below data.我正在尝试从 XPath 访问以下数据的数据。 I wanted to get the Hostname - 192.81.xx.xx and ISP - Random我想获取主机名 - 192.81.xx.xx 和 ISP - 随机

<div class="left">
 <p class="information">
    <span>Hostname</span>
    <span>192.81.xx.xx</span>
 </p>
 <p class="information">
    <span>ISP</span>
    <span>Random</span>
 </p>
</div>

this xpath这个 xpath

//div[@class="left"]//p[@class="information"]//span

will return 4 nodes.将返回 4 个节点。

<span>Hostname</span>
<span>192.81.xx.xx</span>
<span>ISP</span>
<span>Random</span>

based on the HTML that you've shared.基于您共享的 HTML。

you should use find_elements that will return list of web elements.您应该使用将返回 Web 元素列表的find_elements

Also, if you use the above XPath with find_element it will always fetch the first matching node which is <span>Hostname</span>此外,如果您将上述 XPath 与find_element一起使用,它将始终获取第一个匹配节点,即<span>Hostname</span>

to get specific node:获取特定节点:

  1. to get 192.81.xx.xx use the below XPath:要获取192.81.xx.xx ,请使用以下 XPath:

     //div[@class="left"]//p[@class="information"]//span[1]
  2. for random:对于随机:

     //div[@class="left"]//p[2][@class="information"]//span[2]

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

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