[英]selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable error when trying to click an element using Selenium Python
I'm trying to use Python Selenium to download an Excel file by clicking "Export to Excel" in this page: 我正在尝试使用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 https://www.hkex.com.hk/Market-Data/Futures-and-Options-Prices/Equity-Index/Hang-Seng-Index-Futures-and-Options?sc_lang=en#&product=HSI
In Chrome Inspect mode, I think the name of the element is "ete" 在Chrome Inspect模式下,我认为该元素的名称是“ete”
<div class="ete title_right" id="ete">Export to Excel</div>
Here is my code: 这是我的代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
options = Options()
options.add_argument("--headless")
options.add_argument("--disable-gpu")
options.add_argument("--no-sandbox")
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")
driver.find_element_by_id('ete').click()
html = driver.page_source
print(html)
driver.close()
However, element not interactable exception is returned when running my code: 但是,运行我的代码时会返回元素不可交互的异常:
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
[Updates] [更新]
I used Debanjan's method but TimeoutException is returned: 我使用了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:
The page elements take some time to load on this page and the section you're trying to access appears to load last. 页面元素需要一些时间才能在此页面上加载,而您尝试访问的部分似乎最后加载。 What you need to do is put in a wait until the element is visible before attempting to click it.
您需要做的是等待元素可见,然后再尝试单击它。
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")))
After this wait and the element is visible, you can click it. 等待并且元素可见后,您可以单击它。
Here is a site with good info on this: http://allselenium.info/wait-for-elements-python-selenium-webdriver/ 这是一个有关此信息的网站: http : //allselenium.info/wait-for-elements-python-selenium-webdriver/
This error message... 此错误消息...
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
...implies that the desired element was not interactable when you tried to interact with it. ...表示当您尝试与其进行交互时,所需元素无法进行交互。
As you are trying to click()
ideally you should induce WebDriverWait for the desired elementtobeclickable . 当您尝试
click()
您应该将WebDriverWait用于所需的元素,以便可以轻松实现 。 Moreover, there are two(2) elements with text as Export to Excel and I have considered the element which is at the top of the page beside the element with text as FUTURES and you can use the following solution: 此外,还有两(2)个元素,文本为Export to Excel ,我已经考虑了元素位于页面顶部的元素旁边,文本为FUTURES ,您可以使用以下解决方案:
Code Block: 代码块:
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()
Browser Snapshot: 浏览器快照:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.