ultrafunkamsterdam / undetected-chromedriver

Custom Selenium Chromedriver | Zero-Config | Passes ALL bot mitigation systems (like Distil / Imperva/ Datadadome / CloudFlare IUAM)
https://github.com/UltrafunkAmsterdam/undetected-chromedriver
GNU General Public License v3.0
10.08k stars 1.17k forks source link

Headless broke #2083

Open MateusSanchesRodriguez opened 1 week ago

MateusSanchesRodriguez commented 1 week ago

when I run my code with headless turned off it works perfectly, but when I run it with headless it can't find the elements, I asked it to save the html of the page that headless is locking and it returns to the captcha page, so I assume it's not managing to solve the captcha in headless mode, does anyone have a solution?

options = uc.ChromeOptions()

options.add_argument('--no-sandbox') # Bypass OS security model

# options.add_argument('--disable-dev-shm-usage') # Overcome limited resource problems
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
options.add_argument('--disable-gpu')  # Adicione esta linha
#options.add_argument('--headless')
brave_path = r'C:\Users\Teste\hml\selenium\Brave-Browser\Application\brave.exe'
options.binary_location = brave_path

driver = uc.Chrome(options=options)

driver.get('https://www.emailnator.com/')
time.sleep(30)

try:
    driver.find_element('xpath', '//*[@id="custom-switch-domain"]').click()
    print("Clique no botão de login realizado com sucesso!")
except Exception as e:
    html_content = driver.page_source
    # Salva o HTML em um arquivo
    with open("pagina_salva.html", "w", encoding="utf-8") as file:
        file.write(html_content)
        print("HTML da página foi salvo em 'pagina_salva.html'.")

    # Fecha o driver após a execução
    driver.quit()
    print("Erro ao clicar no botão:")
driver.find_element('xpath', '//*[@id="custom-switch-plusGmail"]').click()
mdmintz commented 1 week ago

I patched headless mode of Undetected Chromedriver and NoDriver into https://github.com/seleniumbase/SeleniumBase. (See the UC Mode and CDP Mode docs.) Lots of updates, such as an ad-block option, etc. Here's a script for headless mode: (After pip install seleniumbase)

from seleniumbase import SB

with SB(uc=True, test=True, headless=True, ad_block=True) as sb:
    url = "https://www.browserscan.net/bot-detection"
    sb.activate_cdp_mode(url)
    sb.cdp.flash("Test Results")
    sb.cdp.assert_element('strong:contains("Normal")')
    sb.sleep(0.5)
    sb.cdp.save_screenshot("bscan.png")

bscan