1

I want to click the download button but it does not work.

I tried:

driver.find_element_by_xpath("""//button[contains(text(),'Download')]]""").click()

Error message is

SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[contains(text(),'Download')]]' is not a valid XPath expression.

and tried this code:

driver.find_element_by_xpath("""//*[@id="Download"]""").click()

Error message is:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="Download"]"}

What`s the problem?

Here is my code:

from selenium import webdriver
import time

search = "Acid Value"

driver = webdriver.Chrome('chromedriver.exe')
driver.implicitly_wait(10)
driver.get("https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72")

driver.find_element_by_xpath("//span[contains(text(), 'Chemical and Physical Properties')]").click() # click Chemical and Physical Properties button
driver.find_element_by_xpath("//span[contains(text(), 'Experimental Properties')]").click() # click Experimental Properties button 
    
driver.find_element_by_xpath(f"//span[contains(text(),'{search}')]/parent::li/descendant::span[contains(@class, 'ui-button-text')][2]").click()
#click the Desired properties

driver.implicitly_wait(10) 
    
driver.find_element_by_xpath("""//button[contains(text(),'Download')]]""").click() # error occur here ! 

    
    
driver.implicitly_wait(100) 

driver.find_element_by_xpath("""//button[contains(text(),'SDF')]]""").click() #click the sdf download button
driver.implicitly_wait(100)
8
  • Hi, if you can perhaps show us a bit of the dom and the html that creates and surround this button it may be a bit easier to try and help you. Thank you. Commented Apr 22, 2021 at 0:35
  • do you get error mesage ? your xpath seems incorrect - you need triple """ """ instead of single " " Commented Apr 22, 2021 at 0:38
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. Commented Apr 22, 2021 at 0:38
  • maybe first get all //button and display text in every button - maybe there is no button with Download. Or maybe it has different text, with some extra chars. Or maybe it is in some tag inside button. Commented Apr 22, 2021 at 0:39
  • you have typo - you have too much ] in xpath """//button[contains(text(),'Download')]]""" - you have to remove last ] Commented Apr 22, 2021 at 0:44

1 Answer 1

1
driver.find_element_by_xpath("//div[contains(text(),'Download')]").click()
driver.find_element_by_xpath("//button[@id='Download']").click()

Simple fix to click the download. You had a typo and the wrong html element. It's a div and not button. It was also in another window so you should driver.switch_to.window(driver.window_handles[1]) to that window.

I would also remove the implicit_wait() they only need to be set once. You can use explicit wait which tend to more stable.

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC


wait=WebDriverWait(driver, 20)
driver.get("https://pubchem.ncbi.nlm.nih.gov/classification/#hid=72")
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[contains(text(), 'Chemical and Physical Properties')]"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH,"//span[contains(text(), 'Experimental Properties')]"))).click()
# click Chemical and Physical Properties button
# click Experimental Properties button 
 
search = "Acid Value"
wait.until(EC.element_to_be_clickable((By.XPATH,f"//span[contains(text(),'{search}')]/parent::li/descendant::span[contains(@class, 'ui-button-text')][2]"))).click()
#click the Desired properties
time.sleep(5)
driver.switch_to.window(driver.window_handles[1])
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"button#Download"))).click()
Sign up to request clarification or add additional context in comments.

3 Comments

thanks your answer but "driver.find_element_by_xpath("//div[contains(text(),'Download')]").click()" code occur same error like "selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(),'Download')]"}"
It seems that there was a window switch as well.
I also found that there is problem with switched window/tab :) I think you should mention it in answer because it can be the main problem.

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.