【问题标题】:selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Pythonselenium.common.exceptions.NoSuchElementException:消息:使用 Selenium Python 在 Twitter 中将文本发送到电子邮件字段时无法定位元素错误
【发布时间】:2020-04-22 08:01:32
【问题描述】:

当我尝试使用 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()

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: selenium xpath twitter css-selectors webdriverwait


    【解决方案1】:

    看来你已经很接近了。要将 字符序列 发送到 EmailPassword 字段,您必须为 element_to_be_clickable() 诱导 WebDriverWait,您可以使用关注Locator Strategies

    • 使用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()
      
    • 浏览器快照:

    【讨论】:

      【解决方案2】:

      元素看起来像:

      <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]"]') 
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-23
        • 2020-05-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        相关资源
        最近更新 更多