Alex1234
Alex1234

Reputation: 11

Clicking on button in Selenium Chrome Headless mode does not work

I have a very annoying problem. I actually have found many people with this problem online and various solutions, but none of them work for me.

My code looks like the following, extract of the entire code:


for website in websites:
    chrome_options = webdriver.ChromeOptions()
    chrome_options.add_argument("--no-sandbox")
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("window-size=1500,900");


    driver = webdriver.Chrome(options=chrome_options)
    wait = WebDriverWait(driver, 10)

    # Use specific URL from Container
    driver.get(website["url"])


    element = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//*[@id="CollectorsClub"]/div[2]/div[1]/div[1]/nav/button[2]')))
    driver.execute_script("arguments[0].click();", element)

Which basically opens a website, and then clicks on a specific button on the website. The code works smotth & perfectly if i remove the headless part and the browser manually opens, however with headless I get the following Timeout error: raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message: Stacktrace:

I have also tried clicking the button like this, same error/result:

button = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[@id="CollectorsClub"]/div[2]/div[1]/div[1]/nav/button[2]')))

# Click the button
button.click()

As you can see, I have tried common solutions like setting a window size, I tried different ways of locating the button etc. etc., but it simply does not work.

I would greatly appreciate any inputs.

All the best alex

Upvotes: 1

Views: 54

Answers (0)

Related Questions