How to select element using XPATH syntax on Selenium for Python?

150,165

HTML

<div id='a'>
  <div>
    <a class='click'>abc</a>
  </div>
</div>

You could use the XPATH as :

//div[@id='a']//a[@class='click']

output

<a class="click">abc</a>

That said your Python code should be as :

driver.find_element_by_xpath("//div[@id='a']//a[@class='click']")
Share:
150,165
user2534633
Author by

user2534633

Updated on February 12, 2021

Comments

  • user2534633
    user2534633 over 3 years

    consider following HTML:

    <div id='a'>
      <div>
        <a class='click'>abc</a>
      </div>
    </div>
    

    I want to click abc, but the wrapper div could change, so

    driver.get_element_by_xpath("//div[@id='a']/div/a[@class='click']")
    

    is not what I want

    i tried:

     driver.get_element_by_xpath("//div[@id='a']").get_element_by_xpath(.//a[@class='click']")
    

    but this would not work with deeper nesting

    any ideas?