0

I am using following code to download the excel from this web site, the code was working fine but recently the web page may change so my code doesn't work. It returns this error:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element is not clickable at point (1250,464) because another element obscures it

Here is the code:

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

geckodriver = '/opt/geckodriver'

options = webdriver.FirefoxOptions()
options.add_argument('-headless')

driver = webdriver.Firefox(executable_path=geckodriver, firefox_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, 10).until(EC.element_to_be_clickable((By.ID, 'ete'))).click()
time.sleep(2)

html = driver.page_source

print(html)

I searched the posts in stackoverflow, I found that I can use ActionChains but I dunno how to apply to until(EC.element_to_be_clickable((By.ID, 'ete')))

ActionChains(driver).move_to_element(element).click().perform()
idiot one
  • 314
  • 1
  • 4
  • 11
  • I would recommend removing the add-headless argument so that you can check what is obstructing your click. It might be a sign in page, cookie page, or even just that the website needs to be scrolled before clicking. Depending on that, you would have different solutions. If it is a sign in or cookie page, you would have to enter credentials or press accept respectively. If the problem is that you need to scroll, you would have to do something like element = some WebElement driver.execute_script("arguments[0].scrollIntoView();", element) driver.execute_script("arguments[0].click();", element) – ROOP AMBER Jun 16 '23 at 02:47

1 Answers1

0

Possibly the Cookie Concent Banner obscures the click.


Solution

Click on Accept button first adding the argument --headless=new and you can download the excel inducing WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

  • Code Block:

    from selenium import webdriver
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument('--headless=new')
    options.add_argument("start-maximized")
    driver = webdriver.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, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#ete"))).click()
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352