selenium.common.exceptions.NoSuchElementException:消息:使用 Selenium 将文本发送到 Twitter 中的电子邮件字段时无法定位元素错误 Python
selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python
当我尝试使用 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]"]')
看来你很接近。要将 字符序列 发送到 Email 和 Password 字段,您必须为 element_to_be_clickable()
并且您可以使用以下任一项 :
使用CSS_SELECTOR
:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
def login(self):
bot = self.bot
bot.get('https://twitter.com/login')
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.js-username-field.email-input.js-initial-focus[name='session[username_or_email]']"))).send_keys(self.username)
bot.find_element_by_css_selector("input.js-password-field[name='session[password]']").send_keys(self.password)
run = TwitterBot('jok.moe@hotmail.com', '123456')
run.login()
使用XPATH
:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
class TwitterBot:
def __init__(self,username,password):
self.username = username
self.password = password
options = Options()
options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe'
self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe')
def login(self):
bot = self.bot
bot.get('https://twitter.com/login')
WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='js-username-field email-input js-initial-focus' and @name='session[username_or_email]']"))).send_keys(self.username)
bot.find_element_by_xpath("//input[@class='js-password-field' and @name='session[password]']").send_keys(self.password)
run = TwitterBot('jok.moe@hotmail.com', '123456')
run.login()
浏览器快照:
当我尝试使用 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]"]')
看来你很接近。要将 字符序列 发送到 Email 和 Password 字段,您必须为 element_to_be_clickable()
并且您可以使用以下任一项
使用
CSS_SELECTOR
:from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC class TwitterBot: def __init__(self,username,password): self.username = username self.password = password options = Options() options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe' self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe') def login(self): bot = self.bot bot.get('https://twitter.com/login') WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.js-username-field.email-input.js-initial-focus[name='session[username_or_email]']"))).send_keys(self.username) bot.find_element_by_css_selector("input.js-password-field[name='session[password]']").send_keys(self.password) run = TwitterBot('jok.moe@hotmail.com', '123456') run.login()
使用
XPATH
:from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC class TwitterBot: def __init__(self,username,password): self.username = username self.password = password options = Options() options.binary_location = r'C:\Program Files\Firefox Nightly\firefox.exe' self.bot = webdriver.Firefox(firefox_options=options, executable_path=r'C:\WebDrivers\geckodriver.exe') def login(self): bot = self.bot bot.get('https://twitter.com/login') WebDriverWait(bot, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='js-username-field email-input js-initial-focus' and @name='session[username_or_email]']"))).send_keys(self.username) bot.find_element_by_xpath("//input[@class='js-password-field' and @name='session[password]']").send_keys(self.password) run = TwitterBot('jok.moe@hotmail.com', '123456') run.login()
浏览器快照: