Warm tip: This article is reproduced from serverfault.com, please click

How to login within reddit using Selenium and Python

发布于 2021-01-30 19:38:25

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.

Questioner
zergcore
Viewed
0
PDHide 2021-01-31 03:53:23
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