在 Heroku 上托管时,Selenium 应用程序重定向到 Cloudflare 页面

Posted

技术标签:

【中文标题】在 Heroku 上托管时,Selenium 应用程序重定向到 Cloudflare 页面【英文标题】:Selenium app redirect to Cloudflare page when hosted on Heroku 【发布时间】:2021-02-26 18:09:44 【问题描述】:

我制作了一个使用 selenium 访问网站并获取信息的不和谐机器人,当我在本地运行我的代码时我没有任何问题,但是当我部署到 Heroku 时,我得到的第一个 URL 将我重定向到页面 @ 987654327@.

我试过了:

Selenium webdriver: Modifying navigator.webdriver flag to prevent selenium detection

还有许多其他的设置与我使用的相同:

options = Options()
options.binary_location = os.environ.get("GOOGLE_CHROME_BIN")
options.add_experimental_option("excludeSwitches", ["enable-logging", "enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument("--disable-blink-features=AutomationControlled")
options.add_argument("--headless")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--no-sandbox")
self.driver = webdriver.Chrome(executable_path=os.environ.get("CHROMEDRIVER_PATH"), options=options)
self.driver.execute_cdp_cmd('Network.setUserAgentOverride', 
    "userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/83.0.4103.53 Safari/537.36')

但这不起作用,代码只能在本地运行

PS:本地我在 Windows 上

我被重定向到的页面来源: https://gist.github.com/rafalou38/9ae95bd66e86d2171fc8a45cebd9720c

【问题讨论】:

您被重定向到的页面的内容是什么?会不会是 Cloudflare 的 WAF 挑战你证明你是人类而不是机器人? 这里:gist.github.com/rafalou38/9ae95bd66e86d2171fc8a45cebd9720c 您可能希望在 Cloudflare 页面规则中将 Heroku 机器的 IP 地址列入白名单,这样它就不会触发验证码检查。看到这个答案***.com/questions/50328849/… 【参考方案1】:

如果Selenium 驱动的ChromeDriver 发起的google-chrome 浏览上下文 被重定向到页面...

...这意味着Cloudflare 程序正在阻止您的程序访问AUT(被测应用程序)


分析

Cloudflare 阻止访问的原因可能如下:

Cloudflare 已将您的程序识别为 bot,访问被拒绝。您可以在Can a website detect when you are using selenium with chromedriver? 找到详细讨论。

访问可能会因以下因素而被拒绝:

Cloudflare 正在尝试应对可能的 Dictionary attack。 Cloudflare 将您的系统 IP 列为使用您系统的mining 比特币门罗币的黑名单。

在这些情况下,您最终会被重定向到 captcha 页面。


解决方案

在这些情况下,潜在的解决方案是使用undetected-chromedriver 来初始化Chrome 浏览上下文

undetected-chromedriver 是一个优化的 Selenium Chromedriver 补丁,它不会触发像 Distill Network / Imperva / DataDome / Botprotect.io 这样的反僵尸服务。它会自动下载驱动程序二进制文件并对其进行修补。

代码块:

import undetected_chromedriver as uc
from selenium import webdriver

options = webdriver.ChromeOptions() 
options.add_argument("start-maximized")
driver = uc.Chrome(options=options)
driver.get('https://bet365.com')

替代解决方案

另一种解决方案是通过 Project Honey Pot 网站将您的 IP 地址列入白名单,您可以在标题为 Attention Required one more step captcha CloudFlare Error 的视频中找到详细的端到端流程。

【讨论】:

蜜罐IP没有任何数据:projecthoneypot.org/ip_3.80.128.77 @rafalou38 查看更新后的答案,让我知道状态。 它仍然无法正常工作,我首先尝试了您提出的确切内容,然后尝试了我之前的论点,但它没有帮助,我仍然得到这个 Cloudflare 页面 我在使用driver = uc.Chrome(options=options) 时收到以下错误:*** 发生异常:RuntimeError(注意:显示了完整的异常跟踪,但执行暂停在:)*** 【参考方案2】:

我知道这不是一个实际的解决方案,但有时 Cloudflare 会使用您的 IP 地址按您的位置阻止您。我的代码在我的本地服务器上运行良好,但在 Heroku 中却不行。

事实证明,使用 DebanjanB 提供的解决方案代码是正确的。问题是 Heroku 的服务器运行在与我不同的国家。我通过询问住在另一个国家的朋友尝试用手机进入该网站来证实了这一点。 Cloudflare 阻止了我的朋友要求验证码。

我还没有解决这个问题。我不是专家,解决方法似乎很复杂。我猜代理可以解决它??

如果我能解决它,我会更新。

【讨论】:

是的,我认为代理或 *** 可能是解决此问题的唯一解决方案。

以上是关于在 Heroku 上托管时,Selenium 应用程序重定向到 Cloudflare 页面的主要内容,如果未能解决你的问题,请参考以下文章

Java 聊天程序在本地主机上工作,但在 Heroku 上托管时不能

为啥我的 discord.py 机器人在 Heroku 上托管时没有声音?

Discord.js 我的机器人在 Heroku 上托管时找不到任何父或子频道

在 Netlify 上托管时设置电子邮件

在 unix (nginx) 上托管时 .NET Core 中的 TimeZoneInfo

在 AWS 上托管时,拒绝为目标域生成登录提示的权限