selenium Selify NoSuchElementException:Message:没有这样的元素

rslzwgfq  于 2022-11-10  发布在  其他
关注(0)|答案(2)|浏览(154)

我正在与 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()

enter image description here

fwzugrvs

fwzugrvs1#

您的XPath无效。你不需要这么多引语

driver.find_element_by_xpath("//*[@id='page-download-btn']").click()
7eumitmz

7eumitmz2#

您错过了一次延误。
元素仅在完全呈现并准备好接受单击事件时才应单击。为此,应使用WebDriverWaitexpected_conditions显式等待。
也不需要在URL值之前添加f,也不需要在XPath表达式中添加'"'而不是'
以下代码将为您工作:

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 = "https://pubchem.ncbi.nlm.nih.gov/compound/2078"
driver.get(url)
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "page-download-btn"))).click()

相关问题