selenium.common.exceptions.NoSuchElementException:消息:无法定位元素错误,使用 Selenium Python 将文本发送到 Twitter 中的电子邮件字段 [英] selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python
本文介绍了selenium.common.exceptions.NoSuchElementException:消息:无法定位元素错误,使用 Selenium Python 将文本发送到 Twitter 中的电子邮件字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试使用 Firefox 浏览器在 twitter 网站上自动输入用户名和密码时出现此错误:
I am getting this error when i am trying to enter username and password automatically on twitter website using Firefox browser:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .session[username_or_email]
目前我写的这组代码如下:
The set of code i wrote so far is as follows:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
self.bot = webdriver.Firefox()
def login(self):
bot = self.bot
bot.get('https://twitter.com/')
time.sleep(3)
bot.maximize_window()
bot.implicitly_wait(3)
email = bot.find_element_by_class_name('session[username_or_email]')
password = bot.find_element_by_class_name('session[password]')
email.clear()
password.clear()
email.send_keys(self.username)
password.send_keys(self.password)
run = TwitterBot('jok.moe@hotmail.com', '123456')
run.login()
有人知道如何解决这个问题吗?
Does anyone have any idea how to fix this ?
推荐答案
元素看起来像:
<input class="js-username-field email-input js-initial-focus" type="text" name="session[username_or_email]" autocomplete="on" value="" placeholder="Phone, email or username">
所以你可以这样做:
email = bot.find_element_by_xpath('//input[@name="session[username_or_email]"]')
这篇关于selenium.common.exceptions.NoSuchElementException:消息:无法定位元素错误,使用 Selenium Python 将文本发送到 Twitter 中的电子邮件字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文