简体   繁体   English

无法定位元素 - 硒

[英]Unable to locate element - selenium

I'm trying to make a program that will make proton e-mails by itself.我正在尝试制作一个可以自行制作质子电子邮件的程序。 Everything went smoothly until I had to create code that enters username and password.一切都很顺利,直到我不得不创建输入用户名和密码的代码。 The problem is: It enters password but idk why it doesn't do the same thing with username.问题是:它输入密码,但不知道为什么它不使用用户名做同样的事情。 I tried putting every id I could find and it still didn't work.(I also tried to do something with classes or names but I failed).我试着把我能找到的每一个 id 都放进去,但它仍然没有用。(我也尝试用类或名称做一些事情,但我失败了)。

Here's my code:这是我的代码:

from selenium import webdriver
import time

url = "https://protonmail.com/"
driver = webdriver.Chrome(r'C:\Users\Puhar\Desktop\chromedriver')
driver.get(url)
driver.find_element_by_xpath('//*[@href="signup"]').click()
time.sleep(2)
driver.find_element_by_class_name("panel-heading").click()
time.sleep(2)
driver.find_element_by_id("freePlan").click()
time.sleep(6)
driver.find_element_by_id("domain").send_keys("juzername")
time.sleep(1.5)
driver.find_element_by_id("passwordc").send_keys("usernameForUser")
time.sleep(3.5)
driver.find_element_by_id("password").send_keys('usernameForUser')
time.sleep(1.5)

and here's error:这是错误:

Traceback (most recent call last):回溯(最近一次调用最后一次):

File "C:\\Users\\Puhar\\Desktop\\mejlhekler\\mejlhekler1.py", line 20, in文件“C:\\Users\\Puhar\\Desktop\\mejlhekler\\mejlhekler1.py”,第 20 行,在

driver.find_element_by_id("domain").send_keys("juzername") driver.find_element_by_id("domain").send_keys("juzername")

File "C:\\Users\\Puhar\\AppData\\Roaming\\Python\\Python39\\site- packages\\selenium\\webdriver\\remote\\webdriver.py", line 360, in find_element_by_id文件“C:\\Users\\Puhar\\AppData\\Roaming\\Python\\Python39\\site-packages\\selenium\\webdriver\\remote\\webdriver.py”,第 360 行,在 find_element_by_id

return self.find_element(by=By.ID, value=id_)返回 self.find_element(by=By.ID, value=id_)

Process finished with exit code 1进程以退出代码 1 结束

I think you are using the wrong id tag.我认为您使用了错误的 id 标签。 You were selecting the @protonmail.com dropdown instead of the input tag.您选择的是 @protonmail.com 下拉菜单而不是输入标签。

driver.find_element_by_id("username").send_keys("juzername")

From the log driver.find_element_by_id("domain").send_keys("juzername") , we can know roughly where the error occurred.从日志driver.find_element_by_id("domain").send_keys("juzername") ,我们可以大致知道错误发生在哪里。

See the below source page screenshot, we could get the correct code.看下面的源页面截图,我们可以得到正确的代码。

driver.find_element_by_id("username").send_keys("juzername")

在此处输入图片说明

By the way, //*[@href="signup"] , "panel-heading" are not good idea to locate element you need, because each of them will find many elements.顺便说一句, //*[@href="signup"] , "panel-heading"不是定位您需要的元素的好主意,因为它们中的每一个都会找到许多元素。 I think learning more xpath or cssSelector will help you.我认为学习更多 xpath 或 cssSelector 会对你有所帮助。

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

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