1

I'm trying to automatize the reddit logIn with selenium from python and i'm using the following code in order to do it

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from time import sleep

driver = webdriver.Chrome(executable_path=r'C:\Program Files (x86)\chromedriver.exe')
driver.get("https://www.reddit.com/")

login=driver.find_element_by_link_text("Log In")
login.click()

username = "the-username"          # Enter your username
password = "the-password"                   # Enter your password

def slow_typing(element, text): 
   for character in text: 
      element.send_keys(character)
      sleep(0.3)

def logIn():            # Log In Function.
    try: 
        sleep(15)
        #username_in = driver.find_element_by_class_name("AnimatedForm__textInput")
        username_in = driver.find_element_by_xpath("//*[@id='loginUsername']")
        slow_typing(username_in, username)

        pass_in = driver.find_element_by_xpath("//*[@id='loginPassword']")
        slow_typing(pass_in,password)

        pass_in.send_keys(Keys.ENTER)
        sleep(5)
    except NoSuchElementException:
        print("Llegue aqui xd xd")

logIn()

There's a little more code, but I'm posting a summary so I can tell my problem to you guys. When it is running, it comes to the moment where the input of the username is selected, but it doesn't send the keys. I don't know what to do or change, so I ask for some help here.

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
zergcore
  • 29
  • 5

3 Answers3

1
def logIn():            # Log In Function.
    try:
        driver.switch_to_frame(driver.find_element_by_tag_name('iframe'))
        sleep(5)
        print("hii")
        #username_in = driver.find_element_by_class_name("AnimatedForm__textInput")
        username_in = driver.find_element_by_xpath("//*[@id='loginUsername']")
        slow_typing(username_in, username)

        pass_in = driver.find_element_by_xpath("//*[@id='loginPassword']")
        slow_typing(pass_in, password)

        pass_in.send_keys(Keys.ENTER)
        sleep(5)
        driver.switch_to_default_content()
    except NoSuchElementException:
        print("Llegue aqui xd xd")
        driver.switch_to_default_content()

The login is inside an iframe witch to it first

PDHide
  • 18,113
  • 2
  • 31
  • 46
1

To login within reddit using Selenium and you need to:

  • Induce WebDriverWait for the frame to be available and switch to it.

  • Induce WebDriverWait for the desired element to be clickable.

  • You can use either of the following Locator Strategies:

    • Using XPATH:

      options = webdriver.ChromeOptions() 
      options.add_argument("start-maximized")
      options.add_experimental_option("prefs", { \
          "profile.default_content_setting_values.notifications": 1 
        })
      driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
      driver.get("https://www.reddit.com/")
      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[starts-with(@href, 'https://www.reddit.com/login')]"))).click()
      WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@src, 'https://www.reddit.com/login')]")))
      WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='loginUsername']"))).send_keys("debanjanb")
      driver.find_element(By.XPATH, "//input[@id='loginPassword']").send_keys("zergcore")
      driver.find_element(By.XPATH, "//button[@class='AnimatedForm__submitButton m-full-width']").click()
      
  • Note : You have to add the following imports :

     from selenium import webdriver
     from selenium.webdriver.support.ui import WebDriverWait
     from selenium.webdriver.common.by import By
     from selenium.webdriver.support import expected_conditions as EC
    
  • Browser Snapshot:

reddit_login

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
0

That happens because you don't click inside the input element - below you'll find a small change in your slow typing method, also if have a close look at their code they also have an animation for those fields, clicking the input should solve your issue.

    def slow_typing(element, text):
       element.click()
       for character in text: 
          element.send_keys(character)
          sleep(0.3)

The second recommendation is to use ids instead of XPath whenever you have the chance. Ids give you the best performance and also helps the framework to find the elements easily and not to parse the entire DOM to match the xpath.

Alin Stelian
  • 861
  • 1
  • 6
  • 16