selenium - selenium.common.exceptions.NoSuchElementException:消息:使用 Selenium Python 在 Twitter 中向电子邮件字段发送文本时无法定位元素错误
问题描述
当我尝试使用 Firefox 浏览器在 Twitter 网站上自动输入用户名和密码时出现此错误:
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element: .session[username_or_email]
到目前为止我写的代码集如下:
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()
有谁知道如何解决这个问题?
解决方案
该元素如下所示:
<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]"]')
推荐阅读
- regex - PhpStorm 在两个字符串之间多次搜索和替换
- c# - VSIX 检测已创建解决方案
- ios - 将旧 Xcode 项目转换为新项目
- python - 动态变化的 yticks
- swift - 如何将我的变量正确设置为 Firebase 上的值而不让它重置进度?
- apache-kafka-streams - 是否可以使用spring cloud stream api聚合一个字符串的对象?
- node.js - 节点快递应用的配置管理
- python - 比较元组和列表之间的元素?
- java - 如何使用 Factory 将自定义参数传递给 ViewModel?
- python - 我想让 a1=0, a2=0.. aN=0