【问题标题】:unable to locate element : selenium in python无法定位元素:python中的硒
【发布时间】:2021-02-11 10:20:43
【问题描述】:

我是使用 python 和 selenium 编码的新手。我尝试通过类名在网页中查找元素。

    <input type="email" class="whsOnd zHQkBf" jsname="YPqjbf" 
           autocomplete="username" spellcheck="false" tabindex="0" 
           aria-label="Adresse e-mail ou numéro de téléphone" name="identifier" 
           value="" autocapitalize="none" id="identifierId" dir="ltr" 
           data-initial-dir="ltr" data-initial-value="">

但是每次我尝试都会出现这个错误。

selenium.common.exceptions.NoSuchElementException: 消息:没有这样的元素:无法定位元素:{"method":"css selector","selector":".whsOnd zHQkBf"}

我不明白为什么,因为我已验证并且我认为它不在 iframe 中,所以我不明白为什么我会收到此消息并且我不知道如何管理错误。

这是我的代码和 html 的图片:

My code

The HTML of the page

【问题讨论】:

    标签: python selenium


    【解决方案1】:

    如果没有看到您的代码和 html,很难判断。您确定它不在 iframe 中吗?

    如果没有,您也许可以尝试使用 xpath 来查找元素。 在 chrome 中检查网页时,您可以右键单击该元素并说复制 -> 复制 xpath。我确实建议您查看 xpath,这样您就知道自己在搜索什么。

    编辑: 尝试只使用一个类名,因为 find_element_by_class_name() 只接受一个。

    这个: driver.find_element_by_class_name("zHQkBf")

    而不是这个: driver.find_element_by_class_name("whsOnd zHQkBf")

    【讨论】:

    • 首先感谢您的帮助!我可以重新编辑帖子并加入我的代码和 htlm 的图片。我对 xpath 并不聪明,但我会在 youtube 上观看一些关于此的 tutos。再次检查带有图片的帖子,如果您发现一些错误或不知道,请告诉我...非常感谢
    • Seleniums find_element_by_class_name 据我所知只能取一个类名。因此尝试以下方法,看看它是否有效:driver.find_element_by_class_name("zHQkBf")
    猜你喜欢
    • 1970-01-01
    • 2020-07-23
    • 2019-09-30
    • 1970-01-01
    • 2019-12-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多