Selenium in Headless mode does not find page elements (Python)

Asked

Viewed 107 times

0

My code works normally without the headless activated, but as soon as I activate it stops working, it is as if it did not find the element on the page.

The problem is not in the code of the headless, because I have already sent it to take print and it works normally, the problem if gives from the moment I try to find some element of the page

from selenium import webdriver
from selenium.webdriver.support.ui import Select
from time import sleep

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

navegador = webdriver.Chrome(chrome_options=options)
navegador.get('https://www.instagram.com/')

insta = 'user'
senha2 = 'senha'

sleep(3)

login = navegador.find_element_by_xpath('//*[@id="loginForm"]/div/div[1]/div/label/input')
login.send_keys(insta)

sleep(2)
psw = navegador.find_element_by_name('password')
psw.send_keys(senha2)

sleep(2)
btn_logar = navegador.find_element_by_css_selector('.sqdOP.L3NKy.y3zKF')
btn_logar.click()

sleep(6)
navegador.save_screenshot('insta.png')
  • 3

    Your window may be loaded with dimensions 0x0, and if the page is responsive, you can hide elements to this width. Try to set a fixed size for your window that you know the desired element exists,

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.