【问题标题】:Python selenium not working in healdess modePython selenium 不能在无头模式下工作
【发布时间】:2021-09-30 14:00:29
【问题描述】:

这段代码不能在后台运行,我的意思是无头模式 我以前是后台运行的

options.add_argument('--no-sandbox')
options.add_argument('headless')

这是出错的代码,

def get_data():
    last = ""
    switch_button = driver.find_element_by_xpath(
        '//*[@id="body-content"]/div[2]/div/div[1]/div/div[2]/div[1]/section[2]/div').click()

错误是selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="body-content"]/div[2]/div/div[1]/div/div[2]/div[1]/section[2]/div"}

怎么了?没有无头模式运行良好,当我在后台运行时无法正常工作,如何解决它,有什么办法吗?

【问题讨论】:

  • 这个 options.add_argument('headless') 应该是 options.add_argument('--headless') ,对于这个 NoSuchElementException: Message: no such element: Unable to locate element:请延迟再检查一下。使用相对 xpath 比使用绝对 xpath 更好。

标签: python python-3.x selenium python-3.6


【解决方案1】:

这可能与 question 重复

无论如何,问题是您没有正确编写参数

options.add_argument('--headless')
options.add_argument('--disable-gpu')

从 2018 年 10 月 26 日开始: 您只需执行以下操作即可无头运行:

options.headless = True

(这个在上面添加的question link也有提供,可以阅读他们的solution and comments了解更多)

【讨论】:

  • 同样的错误,带有--headless
  • 你试过options.headless = True吗?另外请确保代码在没有无头的情况下运行良好,以确保问题出在无头时。
猜你喜欢
  • 2022-12-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-12
  • 2019-09-07
  • 2020-11-30
  • 2020-01-10
相关资源
最近更新 更多