selenium.common.异常.ElementNotVisibleException:消息:尝试使用Selenium Python单击元素时发生元素不可交互错误

2024-09-30 06:14:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python Selenium下载Excel文件,方法是单击此页面中的“导出到Excel”:

https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI

在Chrome Inspect模式下,我认为元素的名称是“ete”

<div class="ete title_right" id="ete">Export to Excel</div>

这是我的代码:

^{pr2}$

但是,当运行我的代码时,返回element not interactive异常:

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

[更新]

我使用了Debanjan的方法,但返回TimeoutException:

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

options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
#options.add_argument("--disable-dev-shm-usage")

driver = webdriver.Chrome('/usr/local/bin/chromedriver', chrome_options=options)

driver.get('https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI')
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textrow' and text()='FUTURES']//following::div[@class='ete title_right' and text()='Export to Excel']"))).click()

html = driver.page_source
print(html)
driver.close()
[root@mybox python-learning]# python3.6 web4.py
Traceback (most recent call last):
  File "web4.py", line 16, in <module>
    WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textrow' and text()='FUTURES']//following::div[@class='ete title_right' and text()='Export to Excel']"))).click()
  File "/usr/lib/python3.6/site-packages/selenium/webdriver/support/wait.py", line 80, in until
    raise TimeoutException(message, screen, stacktrace)
selenium.common.exceptions.TimeoutException: Message:

Tags: andtofromimportdivindexdriverselenium
2条回答

此错误消息。。。在

selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable

…意味着当您尝试与所需元素交互时,该元素不可交互。在

当您试图click()理想情况下,您应该诱导WebDriverWait,以获得所需的元素。此外,有两(2)个元素的文本为导出到Excel,我考虑了位于页面顶部、文本为期货元素旁边的元素,您可以使用以下解决方案:

  • 代码块:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument(" disable-extensions")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get('https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI')
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='textrow' and text()='FUTURES']//following::div[@class='ete title_right' and text()='Export to Excel']"))).click()
    
  • 浏览器快照:

download

页面元素需要一段时间才能加载到此页面,而您尝试访问的部分似乎是最后加载的。您需要做的是等待元素可见后再尝试单击它。在

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as ec

WebDriverWait(driver, 30).until(ec.visibility_of_element_located((By.CSS_SELECTOR, "#ete")))

在元素可见之后,您可以等待它。在

这里有一个关于这个的好信息:http://allselenium.info/wait-for-elements-python-selenium-webdriver/

相关问题 更多 >

    热门问题