我正在与 selenium 作斗争
对于URL:https://pubchem.ncbi.nlm.nih.gov/compound/2078
我正在尝试点击下载按钮,但它找不到元素。
我的代码是:
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.chrome.options import Options
from ipykernel import kernelapp as app
import time
options = webdriver.ChromeOptions()
driver_path = 'C:\\Users\\Idener\\Downloads\\chromedriver_win32\\chromedriver.exe'
driver = webdriver.Chrome(driver_path, options=options)
url = f"https://pubchem.ncbi.nlm.nih.gov/compound/2078"
driver.get(url)
driver.find_element_by_xpath("//*[@id='"'page-download-btn'"']").click()
2条答案
按热度按时间fwzugrvs1#
您的XPath无效。你不需要这么多引语
7eumitmz2#
您错过了一次延误。
元素仅在完全呈现并准备好接受单击事件时才应单击。为此,应使用
WebDriverWait
expected_conditions
显式等待。也不需要在URL值之前添加
f
,也不需要在XPath表达式中添加'"'
而不是'
。以下代码将为您工作: