繁体   English   中英

为什么这个 python 代码可以在我的 XUbuntu(Ubuntu 20.04)机器上工作,但不能在我的 Ubuntu 18.04 服务器上工作

[英]Why does this python code work on my XUbuntu (Ubuntu 20.04) machine but not my Ubuntu 18.04 Server

所以我写了这个 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我也试过了。

任何帮助表示赞赏

所以我找到了解决问题的方法:

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

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

之后我会遇到一个名为 selenium 的无模块发现问题,所以我跟着这个:

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

然后我仍然遇到这里描述的错误:

WebDriverException:未知错误:尝试启动 Chrome 浏览器时 DevToolsActivePort 文件不存在

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM