【问题标题】:NoSuchElementException when using Selenium Python [duplicate]使用 Selenium Python 时出现 NoSuchElementException [重复]
【发布时间】:2020-11-04 10:49:06
【问题描述】:

我正在尝试通过单击产品并转到其详细页面来从网站上抓取每个产品的促销信息。当蜘蛛点击产品时,网页会要求它登录,我尝试了以下代码:

    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>

我不确定这是否是问题所在,如果是,如何解决?另外,是否还有其他可能导致该问题的原因?

【问题讨论】:

标签: python selenium selenium-webdriver scrapy selenium-chromedriver


【解决方案1】:

frame里面的登录页面,需要先切换一下:

#switch it first
self.driver.switch_to.frame(driver.find_element_by_id('J_loginIframe'))
self.driver.find_element_by_id('fm-login-id').send_keys('iamgooglepenn')
self.driver.find_element_by_id('fm-login-password').send_keys('HelloWorld1_')

对于登录按钮,您不能使用.find_element_by_class_name,此方法仅适用于单个类名。这个元素有多个类名,所以像下面这样使用.find_element_by_css_selector

#submit button
self.driver.find_element_by_css_selector('.fm-button.fm-submit.password-login').click()

【讨论】:

  • 现在是selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id="J_loginIframe"]"}你在哪里找到框架的ID?
  • @TianheXie 基于回答其他用户的评论,实际上我认为这个问题是指这个链接登录页面:https://login.tmall.com/?spm=a220o.7142085.a2226mz.1.6c997e96viyscY&amp;redirectURL=https%3A%2F%2Fdetail.tmall.com%2Fitem.htm%3F。可以分享真实的网址吗?
  • 不确定您所说的真实网址是什么意思? https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.6.2d375757n6RzBN&amp;id=35550626143&amp;skuId=4372921049418&amp;user_id=1669409267&amp;cat_id=2&amp;is_b=1&amp;rn=e69905ae0d51cc9e426ec93a52d95bcc是我在弹出登录页面时从浏览器复制的网址
  • 蜘蛛的起始 URL 是'https://list.tmall.com/search_product.htm?q=iPad' 然后蜘蛛将点击每个单独的产品以获取更多详细信息。一旦蜘蛛点击,就会弹出登录页面,就是我上面复制的url
【解决方案2】:

弹出窗口将有一个 ID。您可能必须将f'#{popup_id}' 添加到response.url 的末尾。喜欢这个网址:https://stackoverflow.com/questions/62906380/nosuchelementexception-when-using-selenium-python/62906409#62906409。它包含#62906409,因为62906409 是页面中元素的ID。

【讨论】:

  • https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.6.2d375757n6RzBN&amp;id=35550626143&amp;skuId=4372921049418&amp;user_id=1669409267&amp;cat_id=2&amp;is_b=1&amp;rn=e69905ae0d51cc9e426ec93a52d95bcc 这是我从浏览器复制的链接,弹出窗口的 ID 是什么?
  • 您必须检查弹出窗口并找到它的 ID。
  • https://detail.tmall.com/item.htm?spm=a220m.1000858.1000725.6.2d375757n6RzBN&amp;id=35550626143&amp;skuId=4372921049418&amp;user_id=1669409267&amp;cat_id=2&amp;is_b=1&amp;rn=e69905ae0d51cc9e426ec93a52d95bcc#login-form 之类的东西可能会起作用。我刚刚检查了页面。 form 的 ID 是 login-form。此外,弹出窗口似乎没有 ID。
  • 我确定弹出窗口是导致错误的原因。出于某种原因,Selenium 不会将弹出窗口视为 page 中的元素。
  • 那么,e6605ae0d51.. 部分将是表单 ID?我应该做类似 self.driver.get(response.url#{e6605ae0d51.}) 的事情吗?
【解决方案3】:

登录内容似乎嵌套在一个iFrame元素中(如果你一直追踪到顶部,你应该会找到一个iFrameid="sufei-dialog-content"),这意味着你需要切换到那个@ 987654325@ 在选择所需元素之前为该嵌套html,否则它将不起作用。

首先您需要使用driver.switch_to.frame("sufei-dialog-content"),然后使用driver.find_element_by_name() 或任何您拥有的元素选择您的元素。

可以在这里找到类似的问题:Selenium and iframe in html

【讨论】:

    【解决方案4】:

    只是一个简单的错误:

    <div class='input-plain-wrap input-wrap-loginid'>
        <input id='fm-login-id class='fm-text' name='fm-login-id'...>
        event
    </div>
    

    实际上应该是:

    <div class='input-plain-wrap input-wrap-loginid'>
        <input id='fm-login-id' class='fm-text' name='fm-login-id'...>
        event
    </div>
    

    你忘了一个单引号。

    【讨论】:

    • 抱歉,我在发布问题时打错了字。这个 HTML 来自我想从中获取信息的网站。我遇到问题的代码是张贴的第一部分
    • self.driver 是像 Chrome() 一样来自 selenium.webdriver 的 Web 浏览器对象吗?
    • 是的,我在问题中添加了定义 self.driver 的部分
    • 我不确定您所说的弹出 ID 是什么意思?我可以在哪里找到流行 ID 的任何想法?
    【解决方案5】:

    你试过driver.find_element_by_name('fm-login-id')吗?

    【讨论】:

    • 是的,仍然是 NoSuchElementException
    • 你能再看一下页面吗?对我来说,弹出窗口似乎是在 ID 为 id="sufei-dialog-content"iFrame 元素中创建的。如果是这样,您可能需要在尝试获取元素之前使用driver.switch_to.iframe(self,frame reference) 切换到该元素。
    【解决方案6】:

    您应该尝试通过 XPath 查找元素。您只需检查元素,右键单击它并复制它的 XPath。第一个&lt;input ... 的XPath 是//*[@id="fm-login-id"]

    【讨论】:

      猜你喜欢
      • 2022-01-17
      • 1970-01-01
      • 2021-08-16
      • 2022-01-07
      • 2020-06-22
      • 2022-07-06
      • 2019-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多