我无法绕过 selenium webdriver 中的 cloudflare 保护(我正在使用 chrome webdriver)。 保护页面只包含一个复选框,上面写着“验证你是人类”,没有复杂的验证码或任何东西。即使我手动单击该复选框,它也不起作用。 这真的很奇怪,因为我可以在我的主浏览器中轻松完成它。
我已启用此选项:
--disable-blink-features=AutomationControlled
这没有帮助。
甚至尝试了其他一些选择:
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
但还是没用。
我想 selenium chrome 驱动程序中必须有一些选项可以解决这个问题。 还是因为我的chrome版本太旧了?
访问受 Cloudflare 保护的站点时会检测到常规 Selenium,但您可以使用 SeleniumBase UC 模式 等功能来绕过验证码屏幕。这是
pip install seleniumbase
之后并使用 python
运行的示例:
from seleniumbase import SB
with SB(uc=True, test=True) as sb:
url = "https://gitlab.com/users/sign_in"
sb.driver.uc_open_with_reconnect(url, 3)
if not sb.is_text_visible("Username", '[for="user_login"]'):
sb.driver.uc_open_with_reconnect(url, 4)
sb.assert_text("Username", '[for="user_login"]', timeout=3)
这适用于常见的受 CF 保护的网站,如果他们认为您是人类,您根本不必单击复选框。 Cloudflare 验证码有不同类型(例如每个人都必须单击的验证码,或者在执行其他操作(例如单击按钮)后出现的验证码)。
以下是一个站点示例,其中 CF 验证码仅在先执行另一个操作后才会出现。使用
uc_click(selector)
方法绕过验证码:
from seleniumbase import SB
with SB(uc=True, test=True, locale_code="en") as sb:
url = "https://ahrefs.com/website-authority-checker"
input_field = 'input[placeholder="Enter domain"]'
submit_button = 'span:contains("Check Authority")'
sb.driver.uc_open_with_reconnect(url, 1) # The bot-check is later
sb.type(input_field, "github.com/seleniumbase/SeleniumBase")
sb.driver.reconnect(0.1)
sb.driver.uc_click(submit_button, reconnect_time=4)
sb.wait_for_text_not_visible("Checking", timeout=10)
sb.highlight('p:contains("github.com/seleniumbase/SeleniumBase")')
sb.highlight('a:contains("Top 100 backlinks")')
sb.set_messenger_theme(location="bottom_center")
sb.post_message("SeleniumBase wasn't detected!")