无法使用无头 chrome 定位网页上的元素

新手上路,请多包涵

我有一个访问打印机的脚本,当 chrome 正常运行时我的代码工作得很好,但是当它无头运行时,selenium 似乎无法在网页上找到元素。

这是相关代码:

初始化方法:

 def __init__(self, ip_address):
    """ Initialize a new Printer_Webpage object."""
    self.ip_address = ip_address
    chrome_options = Options()
    chrome_options.add_argument("--headless")
    chrome_options.add_argument("--disable-gpu")
    chrome_options.add_argument("--window-size=1920x1080")
    self.browser = webdriver.Chrome(chrome_options=chrome_options)
    # Ignore lack of cert for each printer web page.
    # Otherwise, can't open page.
    self.browser.accept_untrusted_certs = True

登录方式:

 def login(self):
    """Navigates through the login page for the printer."""
    # Open login page
    self.browser.get(f'https://{self.ip_address}/wcd/top.xml')
    # STEPS TO LOGIN:
    # 1) Select 'Administrator' radio button and click.
    self.browser.find_element_by_id('Admin').click()
    # 2) Select Login button and click.
    self.browser.find_element_by_xpath("//input[@type='submit' \
                                        and @value='Login']").click()
    # 3) Select admin (user mode)
    self.browser.find_element_by_id('R_ADM2').click()
    # 4) Select password field and input PASSWORD, then submit.
    password_field = self.browser.find_element_by_id('Admin_Pass')
    password_field.send_keys(PASSWORD)
    password_field.send_keys(Keys.RETURN)

完整的错误信息:

 selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"Admin"}

还有一些其他可能有用的信息:

 (Session info: headless chrome=62.0.3202.94)

(Driver info: chromedriver=2.33.506120 (e3e53437346286c0bc2d2dc9aa4915ba81d9023f),platform=Windows NT 10.0.14393 x86_64)

原文由 Kuba Wernerowski 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.2k
2 个回答

我有同样的问题。您可以截取屏幕截图以了解问题所在。

 driver.get_screenshot_as_file("screenshot.png")

selenium 在正常运行时工作但在无头模式下停止工作的几个原因 -

1)它可能已经切换到移动模板。可以通过更改窗口大小来修复。

 chrome_options.add_argument("--window-size=1920,1080")

2)如果是空白页(截图),可能是因为 SSL 证书无效。(请参阅@Marcel_Wilson 帖子)应该通过 -

 chrome_options.add_argument('--ignore-certificate-errors')
chrome_options.add_argument('--allow-running-insecure-content')

3)网站可能屏蔽了“headless”模式。 (您的屏幕截图可能会显示您无法在正常模式下重新创建的错误)您可以试试这个 -

 user_agent = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.50 Safari/537.36'
options.add_argument(f'user-agent={user_agent}')

但是,如果网站有更强大的拦截方法,上述代码将无法工作。您可以在此处找到更多相关信息 https://intoli.com/blog/making-chrome-headless-undetectable/

原文由 Raj NIgam 发布,翻译遵循 CC BY-SA 4.0 许可协议

我遇到了同样的情况。经研究,下列说法正确的是:

 self.chrome_options = webdriver.ChromeOptions()
self.chrome_options.add_argument("--window-size=1920,1080")
self.chrome_options.add_argument("--disable-extensions")
self.chrome_options.add_argument("--proxy-server='direct://'")
self.chrome_options.add_argument("--proxy-bypass-list=*")
self.chrome_options.add_argument("--start-maximized")
self.chrome_options.add_argument('--headless')
self.chrome_options.add_argument('--disable-gpu')
self.chrome_options.add_argument('--disable-dev-shm-usage')
self.chrome_options.add_argument('--no-sandbox')
self.chrome_options.add_argument('--ignore-certificate-errors')
self.browser = webdriver.Chrome(options=self.chrome_options)

原文由 mushroom_lb 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题