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

How to find an element which its two childs match with two different values?

我想使用 Python selenium 在 Speedtest 网站上找到特定的服务器。例如我想找到host-locationDagupan Cityhost-sponsor的服务器] 是 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>

类似于:

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

演示:

参考文献:

你也可以试试

//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,你可以使用下面的 :

  • 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')]]"))