Unable to locate elements on webpage with headless 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)
推荐答案
如果是SSL证书的问题,您可以使用命令行标志在没有证书的情况下启动Chrome(假设您是这样启动的)。我相信开关是--allow-running-insecure-content
,我从这个列表中找到了here。
这篇关于无法使用无头铬在网页上定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:无法使用无头铬在网页上定位元素


猜你喜欢
- 沿轴计算直方图 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12