selenium.common.exceptions.NoSuchElementException:消息:无法找到元素错误,使用Selenium Python将文本发送到Twitter中的Email字段
问题描述:
当我尝试使用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">
因此您可以执行以下操作:
So you could do something like:
email = bot.find_element_by_xpath('//input[@name="session[username_or_email]"]')