我怎样才能让我的 python selenium 项目在 heroku 上工作 [英] how can I make my python selenium project work on heroku

查看:40
本文介绍了我怎样才能让我的 python selenium 项目在 heroku 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个在本地完美运行的 selenium 项目,它只是一个从网站上抓取数据的脚本.我成功地将脚本部署到 heroku 中,但是当我尝试激活它时,它显示了这些错误.

So I have this selenium project that works perfectly locally , it's just a script that scrapes data from a website . I managed to successfully deploy the script into heroku but when I try to activate it it shows me these errors .

它给出了一个错误,它无法找到它应该从网站上抓取的一些 xpath 元素.这是它显示的错误

it gives an error that it is failing to find some of the xpath elements that it's supposed to scrape from the website. this is the error that it's displaying

引发异常类(消息,屏幕,堆栈跟踪)selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法定位元素:{方法":xpath",选择器"://button[@class='_cs_button _cs_button--sm_cs_button--positive _cs_m-down--none']"}(会话信息:无头 chrome=91.0.4472.77)

raise exception_class(message, screen, stacktrace) selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='_cs_button _cs_button--sm _cs_button--positive _cs_m-down--none']"} (Session info: headless chrome=91.0.4472.77)

在那之后我决定在代码试图找到元素之前打印网站的源代码,因为我想也许元素不存在所以它找不到它们,结果是它的 js来自网站的接收没有被激活,因此之后没有接收到 html.

after that I decided to print the source of the website before the code tried to find the elements, because I thought maybe the elements just weren't there so it couldn't find them and it turns out that the js that it recieves from the website doesn't get activated and so the html isn't recieved after .

我几乎不明白为什么在 heroku 上激活时脚本不会激活从网站收到的 js

I pretty much can't understand why when activated on heroku the script doesn't activate the js recieved from the website

有人可以帮我吗

推荐答案

我猜你在 Heroku 实例中使用的 Python 版本是 >3.8,而本地版本不是.

I am guessing that the Python version you are using in the Heroku instance is >3.8, while the local version is not.

在 Python 3.8 或更高版本中,现在使用 is 比较整数或字符串文字时会引发语法警告.由于库试图将 setting 与空字符串进行比较,因此会引发这样的错误.脚本 firefox_profile.py 将被调用是因为 selenium 将检查您的脚本使用的浏览器版本,这不会对您的脚本造成问题,因为这只是一个警告而不是例外.

In Python 3.8 or above, it will now raise syntax warning when using is to compare integers or string literals. Since the library is trying to compare setting to an empty string, it will raise such an error. The script firefox_profile.py will be called is because selenium will check the browser version that your script uses, which should not cause an issue for your scripts as this is only an warning instead of an exception.

要解决此问题,您可以在 Heroku 实例中降级 Python 版本,或在 selenium 存储库中提交 PR.

To fix this, you may downgrade the Python version in your Heroku instance, or submit a PR in the selenium repository.

更新:

正如我刚才查了官方库,这个问题应固定在最新版本中,你可以找到提交的此处.您可以更新到最新版本的库来修复它.

As I just checked the official repository, the issue should be fixed in the latest version, you can find the commit here. You can update to the latest version of the library to fix it.

这篇关于我怎样才能让我的 python selenium 项目在 heroku 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆