Home > OS >  Selenium webdriver screenshot is different when using '--headless'
Selenium webdriver screenshot is different when using '--headless'

Time:04-29

Using selenium and python, I'm opening a document by making click on a link which opens in the same tab. When I make a screenshot without using '--headless' it takes the document screenshot, but when I activate '--headless' the screenshot is from the previous page (the one where I make click to the document to be opened)

I have tried different browsers and ways (opening in new tab and switching but not working when using '--headless') but not working...

Any idea why '--headless' is behaving like this?

CodePudding user response:

While taking a screenshot ideally you need to induce WebDriverWait for the visibility_of_element_located() of some static and visible element e.g. some <h1> / <h2> element on the desired page and then take the screenshot as follows:

WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "css_visible_element")))
driver.save_screenshot("Federico Albrieu.png")

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

CodePudding user response:

I solved the problem.

I was using the action ActionChains(driver).move_by_offset('xcoordinate', 'ycoordinate).click().perform() to open the desired document. Apparently, while using headless, selenium can't make click in the document coordinate (?) but it can when not using headless.

I tried instead making click with element_to_be_clickable((By.XPATH, 'XPATH'))).click() and it works when using headless.

Thanks!

  • Related