0

I want to use Python selenium to find the specific server on Speedtest website. For example, I want to find the server which its host-location is Dagupan City and host-sponsor is USATV One Inc. Below is how the html code looks like. There will be lots of the same classname inside. What should I do to match with two different values from this? Thanks!

<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>
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
LaoDa581
  • 563
  • 4
  • 18

3 Answers3

1

Something like:

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

Demo:

enter image description here

References:

Dmitri T
  • 159,985
  • 5
  • 83
  • 133
  • I use "find_elements_by_xpath" to implement you code and it works! However, it will get a list includes two elements. Is that normal? I can't find another element on my website. – LaoDa581 Jun 12 '19 at 12:35
  • Wouldn't `a[contains(., 'Dagupan City') and contains(., 'USATV One Inc')]` work just as well? – Greg Burghardt Jun 12 '19 at 17:12
0

you can also try like

//span[@class='host-sponsor'][contains(text(),'USATV One Inc')]/parent::a/span[contains(text(),'Dagupan City')]/parent::a
murali selenium
  • 3,847
  • 2
  • 11
  • 20
0

To find the server with respect to its children class attribute host-location e.g. Dagupan City and host-sponsor e.g. USATV One Inc as the elements are dynamic elements, you need to induce WebDriverWait for the desired elements and you can use the following 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')]]"))
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352