selenium.common.exceptions.NoSuchElementException:消息:无法找到元素错误,使用Selenium Python将文本发送到Twitter中的Email字段 [英] selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python

查看:99
本文介绍了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]"]') 

这篇关于selenium.common.exceptions.NoSuchElementException:消息:无法找到元素错误,使用Selenium Python将文本发送到Twitter中的Email字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆