0

The code below, when the headless option is commented out, works perfectly, it manages to click on the VIEW ALL button on the site, but when we include the headless option, the error is displayed: Message: element click intercepted: Element is not clickable at point. I've researched and tried everything imaginable, nothing works. Anyone with any good ideas? thanks

import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from webdriver_manager.chrome import ChromeDriverManager

custom_user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36"  # noqa

options = Options()
options.add_argument('--headless')
options.add_argument("start-maximized")
options.add_argument(f'user-agent={custom_user_agent}')

url = 'https://www.etf.com/SPY'

driver = webdriver.Chrome(options=options, service=Service(ChromeDriverManager().install()))  # noqa
driver.get(url)
time.sleep(3)

body = driver.find_element(By.XPATH, '/html/body')
body.send_keys(Keys.PAGE_DOWN)
body.send_keys(Keys.PAGE_DOWN)
body.send_keys(Keys.PAGE_DOWN)
body.send_keys(Keys.PAGE_DOWN)
time.sleep(3)

driver.find_element(By.XPATH, '//*[@id="holdings"]/div[2]/div[1]/div[1]/div[2]/div/div/div[1]/div[2]/div[3]/button').click()  # noqa
input('wait..')

2 Answers 2

2

With below user agent was able to successfully click on View All button.

Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36

You can get user agent with below line.

user_agent = driver.execute_script("return navigator.userAgent;")

No need to write body.send_keys(Keys.PAGE_DOWN) 4 times to scroll to the element. Instead locate the chart element and use scrollIntoView option. Can use get_screenshot... to track the process.

driver.get_screenshot_as_file("filname.png")

Code:

from selenium import webdriver
from selenium.webdriver.common.by import By
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options

options = Options()
options.add_argument('--headless')
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.5615.138 Safari/537.36'
options.add_argument(f'user-agent={user_agent}')

driver = webdriver.Chrome(options=options,service=Service(ChromeDriverManager().install()))

driver.get("https://www.etf.com/SPY")

wait = WebDriverWait(driver,30)

# Scroll to the chart where View All button exist
chart = wait.until(EC.visibility_of_element_located((By.XPATH,"//div[@data-prop-label='Top 10 Holdings']")))
driver.execute_script("arguments[0].scrollIntoView(true)",chart)

# Locate View All button and using Javascript to click on that element to avoid "element click intercepted" exception. 
element = wait.until(EC.element_to_be_clickable((By.XPATH,"//span[text()='View All']")))
driver.execute_script("arguments[0].click()",element)
Sign up to request clarification or add additional context in comments.

1 Comment

pmadhu, thank you very much for this, as you may have noticed I am a beginner and your answer not only solved my problem, it also cleared my mind on other issues. Thank you very much
0

This is a very common and hard to debug problem, but in my experience there are 3 common reasons for this to happen:

  • the element is partially or completely covered by another element and you might wand to make a screenshot to see if this is the case
  • the element is not in the view area and you might want to scroll to the element position before trying to click it
  • the element is temporarily not clickable and this could for example be caused by the element to be only enabled when certain conditions are met

PS: this type of problem was actually one of multiple reasons my I moved from selenium to Playwright.

1 Comment

Hello doberkofler, thanks for your comment. Really through my research, I realized that the causes can be countless, I have suffered a little to do things that should be easy, but after seeing pmadhu friend's answer, I conclude that it is just lack of practice/knowledge of the possibilities. The solution was simple.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.