简体   繁体   English

无法定位元素:python 中的 selenium

[英]unable to locate element : selenium in python

I'm a newbie in coding with python and selenium.我是使用 python 和 selenium 编码的新手。 I try to find an element in a web-page by its class name.我尝试通过其 class 名称在网页中查找元素。

    <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="">

But each time I've tried I have this error.但是每次我尝试过时都会出现这个错误。

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".whsOnd zHQkBf"} selenium.common.exceptions.NoSuchElementException:消息:没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“.whsOnd zHQkBf”}

I don't understand why because I verified and I think that it's not in an iframe so I don't understand why do I have this message and I don't know how to manage the error.我不明白为什么,因为我已验证并且我认为它不在 iframe 中,所以我不明白为什么我会收到此消息并且我不知道如何管理错误。

Here are the pictures of my code and the html:这是我的代码和 html 的图片:

My code我的代码

The HTML of the page页面的 HTML

Hard to tell without seeing your code and the html.如果没有看到您的代码和 html,很难分辨。 Are you sure it is not in a iframe?您确定它不在 iframe 中吗?

If not you can perhaps try to use xpath to find the element.如果没有,您也许可以尝试使用 xpath 来查找元素。 When inspecting the webpage in chrome you can right click the element and say copy -> copy xpath.在 chrome 中检查网页时,您可以右键单击该元素并说复制 -> 复制 xpath。 I do recommend looking into xpath, so you know what you are searching for.我建议您查看 xpath,这样您就知道自己在搜索什么。

Edit: Try using only one class name, since find_element_by_class_name() only accepts one.编辑:尝试只使用一个 class 名称,因为 find_element_by_class_name() 只接受一个。

This: driver.find_element_by_class_name("zHQkBf")这个:driver.find_element_by_class_name("zHQkBf")

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM