idiot one
idiot one

Reputation: 389

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:

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"

<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:

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:

Upvotes: 0

Views: 365

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193308

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. 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:

  • 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:

download

Upvotes: 0

RKelley
RKelley

Reputation: 1119

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/

Upvotes: 0

Related Questions