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

时间:2019-06-12 11:06:18

标签: python selenium xpath webdriverwait xpath-1.0

我想使用Python硒在Speedtest网站上找到特定的服务器。例如,我要查找其主机位置 Dagupan City host-sponsor USATV One Inc < / strong>。 以下是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>

3 个答案:

答案 0 :(得分:1)

类似的东西:

//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

参考文献:

答案 1 :(得分:0)

您也可以尝试像

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

答案 2 :(得分:0)

根据子服务器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')]]"))