【问题标题】:Why does this python code work on my XUbuntu (Ubuntu 20.04) machine but not my Ubuntu 18.04 Server为什么这个 python 代码可以在我的 XUbuntu (Ubuntu 20.04) 机器上运行,但不能在我的 Ubuntu 18.04 服务器上运行
【发布时间】:2021-04-30 12:33:38
【问题描述】:

所以我编写了这个 python 代码,它应该会自动更新我在德国 roomshare 网站上的广告:

from time import sleep
from selenium import webdriver
import sched, time
from selenium.webdriver import FirefoxOptions
import datetime


s = sched.scheduler(time.time, time.sleep)


def click_button_by_id(button_s, browser):
    button = browser.find_element_by_id(button_s)
    button.click()



def refresh_ads(sc):

    opts = webdriver.ChromeOptions()
    opts.add_argument("--headless")
    opts.add_argument("--no-sandbox")
    opts.add_argument("--disable-dev-shm-usage")
    browser = webdriver.Chrome(options = opts)
    browser.get('https://www.wg-gesucht.de')
    try:
        click_button_by_id("cmpbntyestxt", browser)
    except:
        pass

    browser.implicitly_wait(5)
    login_link = browser.find_element_by_xpath('//*[@id="headbar_wrapper"]/div[2]/a[2]')
    login_link.click()

    sleep(2)

    username_input = browser.find_element_by_id("login_email_username")
    password_input = browser.find_element_by_id("login_password")

    username_input.send_keys("MY_USERNAME")
    password_input.send_keys("MY_PASSWORD")

    login_button = browser.find_element_by_id("login_submit")
    login_button.click()

    sleep(2)

    browser.get('https://www.wg-gesucht.de/angebot-bearbeiten.html?action=update_offer&offer_id=xxxxxxxx')
    click_button_by_id("set_today_date",browser)
    click_button_by_id("update_offer",browser)

    sleep(2)

    browser.get('https://www.wg-gesucht.de/angebot-bearbeiten.html?action=update_offer&offer_id=xxxxxxxx')
    click_button_by_id("set_today_date",browser)
    click_button_by_id("update_offer",browser)

    print("Refreshed adds at:",datetime.datetime.now().strftime("%c"))

    sleep(2)

    browser.close()

    s.enter(1800, 1, refresh_ads, (sc,))

s.enter(1, 1, refresh_ads, (s,))
s.run()

使用它在我的家用机器上完全可以正常工作:

Ubuntu 20.04
Python 3.8.5
铬 88.0.4324.96
Chromedriver 1:85.0.4183.83-0ubuntu0.20.04.2

使用此功能在我的服务器上崩溃时:

Ubuntu 18.04
Python 3.8.7(之前是 3.6.9。我更新了)
铬 87.0.4280.66
Chromedriver 87.0.4280.66-0ubuntu0.18.04.1

还有这个错误信息:

  File "wg_gesucht_bot.py", line 66, in <module>
    s.run()
  File "/usr/lib/python3.6/sched.py", line 154, in run
    action(*argument, **kwargs)
  File "wg_gesucht_bot.py", line 23, in refresh_ads
    browser = webdriver.Chrome(options = opts)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py", line 81, in __init__
    desired_capabilities=desired_capabilities)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.6/dist-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.SessionNotCreatedException: Message: session not created
from tab crashed
  (Session info: headless chrome=87.0.4280.66)

我已经尝试过使用Firefox,它实际上是我最初使用的,也是我仍然导入FirefoxOptions的原因。因为否则它将无法正常工作(它会启动但不会在网页本身上走得太远)但这是我之后可以解决的问题。

有这个:https://stackoverflow.com/a/40379664/10811865,但更新到 5 年前的版本对我来说没有多大意义

还有这个:https://stackoverflow.com/a/59191325/10811865 我也试过了。

感谢任何帮助

【问题讨论】:

  • 你试过phantomjs浏览器吗?
  • @Jawad 我没有尝试过。但我找到了一个不同的解决方案,我将其发布为答案。出于好奇;为什么我要使用已弃用的浏览器?肯定有 JS 浏览器还在更新中

标签: python selenium ubuntu selenium-webdriver selenium-chromedriver


【解决方案1】:

于是我找到了解决问题的办法:

首先,我必须将 python 更新到 3.8.5,我使用这个:

https://linuxize.com/post/how-to-install-python-3-8-on-ubuntu-18-04/

之后我会遇到一个名为 selenium 的 no module found 问题,所以我遵循了这个:

https://shashanksrivastava.medium.com/how-to-fix-no-module-named-selenium-error-in-python-3-da3fd7b61485 手动安装

然后我还是遇到了这里描述的错误:

WebDriverException: unknown error: DevToolsActivePort file doesn't exist while trying to initiate Chrome Browser

我已经实施了那些修复,所以它们并没有太多帮助,但杀死我机器上的所有 chromium 和 chromedriver 进程为我解决了这个问题。

最后我不得不添加这行代码:

opts.add_argument('window-size=1920x1080');

这样我的代码即使在无头模式下也能正常运行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-05
    • 2016-12-13
    • 2015-07-12
    • 2014-09-04
    • 2018-08-27
    • 2020-04-21
    • 1970-01-01
    相关资源
    最近更新 更多