I’m doing automation for my company and there’s a step where I need to log in, but this login part requires Cloudfare verification. I turned to chatgpt to try to help me but I didn’t get much result. I started doing this type of code recently, so I don’t have much knowledge about the area.
from tkinter import Tk
from tkinter.filedialog import askopenfilename
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.service import Service
from selenium_stealth import stealth
from time import sleep
usuario = 'XXXXX'
senha = 'XXXXX'
Tk().withdraw()
arquivo_csv = askopenfilename(filetypes=[('Arquivos CSV', '*.csv')])
chrome_driver_path = "C:/WebDriver/ChromeDriver/chromedriver.exe"
service = Service(executable_path=chrome_driver_path)
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--disable-blink-features=AutomationControlled") # Remove a flag de automação
driver = webdriver.Chrome(service=service, options=chrome_options)
stealth(driver,
languages=["en-US", "en"],
vendor="Google Inc.",
platform="Win32",
webgl_vendor="Google Inc.",
renderer="ANGLE (Google Inc.)",
fix_hairline=True,
)
driver.get('https://oikos.corbee.com.br/')
sleep(5)
driver.switch_to.frame(driver.find_element(By.TAG_NAME, "iframe"))
campo_login = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.NAME, "usuario")))
campo_login.send_keys(usuario)
campo_senha = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, "//input[@type='password']")))
campo_senha.send_keys(senha)
sleep(60)
driver.quit()
4
Until your company provides a workaround for the QA Team, you can bypass the Cloudflare CAPTCHA using SeleniumBase UC Mode. The following example is custom made for your site:
from seleniumbase import SB
with SB(uc=True) as sb:
url = "https://oikos.corbee.com.br/login.php"
sb.uc_open_with_reconnect(url, 6)
sb.uc_gui_click_captcha()
sb.type('[name="usuario"]', "username")
sb.type("#password", "password")
# sb.click("#logar")
sb.sleep(4)
Here’s another one that uses the Driver()
format:
from seleniumbase import Driver
driver = Driver(uc=True)
try:
url = "https://oikos.corbee.com.br/login.php"
driver.uc_open_with_reconnect(url, 6)
driver.uc_gui_click_captcha()
driver.type('[name="usuario"]', "username")
driver.type("#password", "password")
# driver.click("#logar")
driver.sleep(4)
finally:
driver.quit()
If running on Linux is a must, use the SB()
format. You can access raw selenium
methods from there using sb.driver
.