Reputation: 23
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)
Upvotes: 1
Views: 1083
Reputation: 9969
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()
Upvotes: 1