如何使用Selenium Python绕过Cloudflare浏览器检查

10

我正在尝试使用Python的Selenium访问一个网站。 但该网站一直在通过Cloudflare进行持续检查。 没有其他页面显示。

请在此处查看截图。

enter image description here

我已经尝试了不被检测到的Chrome,但它根本不起作用。
3个回答

7

您所说的未检测到的Chrome是指未检测到的chromedriver吗?

不管怎样,对我来说,未检测到的chromedriver可用:

未检测到的chromedriver

Github: https://github.com/ultrafunkamsterdam/undetected-chromedriver

pip install undetected-chromedriver

获取Cloudflare保护站点的代码:

import undetected_chromedriver as uc
driver = uc.Chrome(use_subprocess=True)
driver.get('https://nowsecure.nl')

我的观点

enter image description here enter image description here


快速设置代码,可登录您的Google帐户:

Github: https://github.com/xtekky/google-login-bypass

import undetected_chromedriver as uc
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

#  ---------- EDIT ----------
email = 'email\n' # replace email
password = 'password\n' # replace password
#  ---------- EDIT ----------

driver = uc.Chrome(use_subprocess=True)
wait = WebDriverWait(driver, 20)
url = 'https://accounts.google.com/ServiceLogin?service=accountsettings&continue=https://myaccount.google.com%3Futm_source%3Daccount-marketing-page%26utm_medium%3Dgo-to-account-button'
driver.get(url)


wait.until(EC.visibility_of_element_located((By.NAME, 'identifier'))).send_keys(email)
wait.until(EC.visibility_of_element_located((By.NAME, 'password'))).send_keys(password)
print("You're in!! enjoy")

# [ ---------- paste your code here ---------- ]

嗨,谢谢,但是像我说的那样,未检测到的chromedriver对我不起作用。我已经再次检查过了。 - JamesHorab
请查看以下链接:https://dev59.com/S1MI5IYBdhLWcg3wUZud,或者https://blog.m157q.tw/posts/2020/09/11/bypass-cloudflare-detection-while-using-selenium-with-chromedriver/,您可能会在其中找到答案。 - xtekky
1
@xtekky undetected_chromedriver v1 目前存在漏洞,我们仍在努力将 undetected_chromedriver 与 Selenium v4.x 集成。 - undetected Selenium
谢谢!它对我来说完美地运行了。通过添加uc.Chrome(use_subprocess=True),它开始正常工作。此外,我从selenium.webdriver.common.by导入By,并使用XPath查找元素:userTxtBox = self.driver.find_element(By.XPATH,'//*[@id="ctl00_ContentPlaceHolder_UserNameTextBox"]')。 - mavi

4

https://github.com/seleniumbase/SeleniumBase有一个未检测到的chromedriver模式(--uc / uc=True)。

在执行pip install seleniumbase之后,像这样的脚本将绕过带有Cloudflare旋转门的网站。

from seleniumbase import SB

with SB(uc=True) as sb:
    sb.open("https://nowsecure.nl/#relax")
    sb.sleep(2)
    if not sb.is_text_visible("OH YEAH, you passed!", "h1"):
        sb.get_new_driver(uc=True)
        sb.open("https://nowsecure.nl/#relax")
        sb.sleep(2)
    sb.assert_text("OH YEAH, you passed!", "h1", timeout=4)
    sb.sleep(2)

(使用sb.driver来访问原始驱动程序。)
(还有一个简单的driver格式:)
from seleniumbase import Driver
import time

driver = Driver(uc=True)
driver.get("https://nowsecure.nl/#relax")
time.sleep(4)
driver.quit()

它可以绕过大多数网站的检测。


1

这种简单的方法很有用:

from selenium import webdriver
import time 

browser = webdriver.Chrome()
browser.get(url)

# sleep to wait pass
time.sleep(3) 

html_source = browser.page_source
print(html_soup)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接