40

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?

7
  • 2
    do you have to use xpath? cant you use a css selector? #a .click ? Commented Sep 26, 2013 at 17:54
  • Well change how? Does it gain an ID? Does it's position change? Does it place that a element into another element? Commented Sep 26, 2013 at 17:55
  • sry didnt understand your comment, the selector #a .click will get an element with id a and look inside for an element with class click. if the wrapper div changes it will still work. Commented Sep 26, 2013 at 17:59
  • driver.get_element_by_xpath("//a[@class='click']")? Commented Sep 26, 2013 at 18:00
  • There is possibility for several <a class='click'>abc</a> elements in the page, so doesn't solve my case Commented Sep 26, 2013 at 18:33

4 Answers 4

68

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']")
Sign up to request clarification or add additional context in comments.

5 Comments

Why so ? Probably I didn't get your point. Could you a bit more clear?
'abc' in real case would be 'profile' which in Chinese would be '輪廓'
it's not your fault Arup.. the OP seems to be very unclear on what he's asking.
I never agree with matching css or xpath based on text. Since you changed it, +1
8

In the latest version 4.x, it is now:

elem = driver.find_element(By.XPATH, "/html/body/div[1]/div/div[2]/h2/a")

To import By:

from selenium.webdriver.common.by import By

Comments

0

Selenium 4.X ~
In the real-world scenario, we need to filter the XPATH to extract the data.
For example, below Amazon's website:
https://www.amazon.ca/gp/product/B09NZ72B5X/ref=ewc_pr_img_1?smid=A3VU3XJC72QOSZ&th=1

while extracting the XPATH for an item, we get "/text()" in the end. I removed it and it worked for me.

XPATH

price = driver.find_element(By.XPATH, '//*[@id="corePriceDisplay_desktop_feature_div"]/div[1]/span[1]/span[2]/span[2]')
print(price.text)

Comments

-2

Check this blog by Martin Thoma. I tested the below code on MacOS Mojave and it worked as specified.

> def get_browser():
>     """Get the browser (a "driver")."""
>     # find the path with 'which chromedriver'
>     path_to_chromedriver = ('/home/moose/GitHub/algorithms/scraping/'
>                             'venv/bin/chromedriver')
>     download_dir = "/home/moose/selenium-download/"
>     print("Is directory: {}".format(os.path.isdir(download_dir)))
> 
>     from selenium.webdriver.chrome.options import Options
>     chrome_options = Options()
>     chrome_options.add_experimental_option('prefs', {
>         "plugins.plugins_list": [{"enabled": False,
>                                   "name": "Chrome PDF Viewer"}],
>         "download": {
>             "prompt_for_download": False,
>             "default_directory": download_dir
>         }
>     })
> 
>     browser = webdriver.Chrome(path_to_chromedriver,
>                                chrome_options=chrome_options)
>     return browser

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.