如何找到其两个子元素与两个不同值匹配的元素?

2024-04-25 04:07:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Python selenium在Speedtest网站上查找特定的服务器。例如,我想找到其主机位置大沽市主机赞助商USATV One Inc的服务器。 下面是html代码的样子。里面会有很多相同的类名。我应该怎么做来匹配这两个不同的值? 谢谢!你知道吗

<ul data-view-name="serverCollection" data-view-cid="view34" class=""><li data-model-cid="c184">
  <a data-server-id="11886" data-https-host="1">
      <span class="host-location">
        Dagupan City
      </span>
      <span class="host-sponsor">
        USATV One Inc
      </span>


  </a>
</li><li data-model-cid="c185">
  <a data-server-id="25314" data-https-host="1">
      <span class="host-location">
        Park City, Utah
      </span>
      <span class="host-sponsor">
        Utah Broadband
      </span>


  </a>
</li><li data-model-cid="c186">
  <a data-server-id="14515" data-https-host="1">
      <span class="host-location">
        Nagaoka
      </span>
      <span class="host-sponsor">
        CanopusAzusa
      </span>


  </a>
</li><li data-model-cid="c187">
  <a data-server-id="14890" data-https-host="1">
      <span class="host-location">
        Jakarta
      </span>
      <span class="host-sponsor">
        PT. Aplikanusa Lintasarta
      </span>


  </a>
</li></ul>

Tags: https服务器idhostdatamodelserverlocation
3条回答

比如:

//a[span[@class='host-location' and contains(text(),'Dagupan City')] and span[@class='host-sponsor' and contains(text(),'USATV One Inc')]]

演示:

enter image description here

参考文献:

你也可以试试看

//span[@class='host-sponsor'][contains(text(),'USATV One Inc')]/parent::a/span[contains(text(),'Dagupan City')]/parent::a

要查找与其子项class属性^{host-location有关的服务器,例如,达古班市host-sponsor例如,USATV One Inc由于这些元素是动态元素,因此需要为所需的元素归纳WebDriverWait,并且可以使用以下Locator Strategy

  • XPath:

    element = WebDriverWait(driver,15).until(lambda driver: driver.find_element_by_xpath("//a[span[@class='host-location' and contains(.,'Dagupan City')]]") and driver.find_element_by_xpath("//a[span[@class='host-sponsor' and contains(.,'USATV One Inc')]]"))
    

相关问题 更多 >