簡體   English   中英

我想在 Python 中單擊 Selenium 中的下載按鈕

[英]I want to click download button in Selenium in Python

我想點擊下載按鈕,但它不起作用。

我試過了:

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

錯誤信息是

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

並嘗試了這段代碼:

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

錯誤信息是:

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

有什么問題?

這是我的代碼:

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)
driver.find_element_by_xpath("//div[contains(text(),'Download')]").click()
driver.find_element_by_xpath("//button[@id='Download']").click()

簡單修復點擊下載。 你有一個錯字和錯誤的 html 元素。 這是一個 div 而不是按鈕。 它也在另一個 window 中,因此您應該將 driver.switch_to.window(driver.window_handles[1]) 轉換為 window。

我還將刪除它們只需要設置一次的implicit_wait()。 您可以使用更穩定的顯式等待。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM