1

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

2 Answers2

0

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
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Hey! Thanks for your answer. Is still happening the same. It works without headless but when i use headless is not finding the opened page. It seems like selenium is not reaizing that the opened document was opened in the same tab and is still focused in the previous page... its weird – Federico Albrieu Apr 27 '22 at 16:16
  • Did you happen to change Selenium focus to the new window? – undetected Selenium Apr 27 '22 at 17:03
  • Yes, I've already tried it. Without headless it works fine changing focus, opening the document in new tab, in same tab, and different ways, but when using headless, the its like it's not detecting that the document was opened. Maybe its important to say. When clicking the document, the new tab is a pdf preview where you can print it – Federico Albrieu Apr 27 '22 at 19:12
0

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!

Yashar
  • 762
  • 8
  • 16