selenium.common.exceptions.NoSuchElementException: Message: Unable to locate element error sending text to Email field in twitter with Selenium Python

前端 未结 2 2004
Happy的楠姐
Happy的楠姐 2020-12-07 06:28

I am getting this error when i am trying to enter username and password automatically on twitter website using Firefox browser:

selenium.common.exceptions.N         


        
相关标签:
2条回答
  • 2020-12-07 06:37

    The element looks like:

    <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]"]') 
    
    0 讨论(0)
  • 2020-12-07 06:38

    Seems you were pretty close. To send a character sequence to the Email and Password fields, you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

    • Using 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()
      
    • Using 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()
      
    • Browser Snapshot:

    twitter_login

    0 讨论(0)
提交回复
热议问题