python - 使用 Selenium Python 时出现 NoSuchElementException
问题描述
我正在尝试通过单击产品并转到其详细页面来从网站上抓取每种产品的促销信息。当蜘蛛点击产品时,网页会要求它登录,我尝试了以下代码:
def __init__(self):
self.driver = webdriver.Chrome(executable_path = '/usr/bin/chromedriver')
...
def start_scraping(self, response):
self.driver.get(response.url)
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')
self.driver.find_element_by_class_name('fm-button fm-submit password-login').click()
...
但是,当我运行它时会出现 NoSuchElementException。
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="fm-login-id"]"}
'spider_exceptions/NoSuchElementException': 14,
登录页面的HTML如下:
<div class='input-plain-wrap input-wrap-loginid'>
<input id='fm-login-id' class='fm-text' name='fm-login-id'...>
event
</div>
所以,我很确定 id 应该是“fm-login-id”。我能想到的可能导致这个问题的原因是这个登录页面是一个弹出窗口。
基本上,它会在主页的中间弹出。查看站点的 HTML,可以看到登录类型似乎是一个新的 HTML 窗口
<!DOCTYPE html>
<html>event
....
<\html>
我不确定这是否是问题,如果是,如何解决?另外,是否还有其他可能导致该问题的原因?
解决方案
弹出窗口将有一个 ID。您可能必须添加f'#{popup_id}'
到response.url
. 喜欢这个网址:https://stackoverflow.com/questions/62906380/nosuchelementexception-when-using-selenium-python/62906409#62906409
。它包含#62906409
因为62906409
是页面中元素的 ID。
推荐阅读
- django - 如何将 Django 查询过滤器放在 ON 子句而不是 WHERE 子句中?
- c# - 在 C# 中使用 Harmon.ie SDK
- android - 如何使用 Kotlin 工具在 Android Studio 中指定从一种形式转换为另一种形式
- c++ - 将 HyperLedger Fabric 与 C++ 应用程序一起使用
- react-native - 具有动态资源的图像源上的 500 错误反应本机
- scala - 如何在 Idea IntelliJ 编辑器中使用 Scala Jackson 库执行基本 Json 馈送器
- nativescript - imageSourceModule.fromFile(path) 在 nativescript 中返回 null
- python - 在 Python 中循环遍历数据框的更优雅的方式
- deployment - 如何在 siddhi 应用程序中使用 deployment.yaml 变量?
- angular - Angular 7 - 创建 Angular 应用程序时出错