Home > Software design >  Login Automation Using Selenium Not Working Properly
Login Automation Using Selenium Not Working Properly

Time:11-17

I have built a login Automator using Selenium, and the code executes without errors but the script doesn't login. The page is stuck at login page, email and password are entered, but login is not completed. enter image description here

I have tried 2 ways to login:

  1. By clicking on Login through Click ()
e = self.driver.find_element(By.XPATH, "//div[text()='Login']")
e.click()
  1. Using Enter in password area
password_element.send_keys(Keys.ENTER)

But neither of them logs me in, even though I can see the button being clicked, and name and password being entered. I also tried adding a wait time, but the problem is not solved. What an I doing wrong?

Here is the code:


import pandas as pd
from selenium import webdriver
from selenium.webdriver import Firefox
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys

class QuoraScraper:

    def __init__(self):
        self.driver = ''
        self.dataframe = ''
        self.credentials = {
            'email': 'email',
            'password': 'password'
        }
        self.questions = []
        self.answers = []


    def start_driver(self):
        options = Options()
        options.binary_location = r'C:\Program Files\Mozilla Firefox\firefox.exe'
        #profile_path = r'C:\Users\A\AppData\Roaming\Mozilla\Firefox\Profiles\profile.default'
        #r"C:\Users\I\Downloads\geckodriver-v0.32.0-win32",
        self.driver = webdriver.Firefox(options=options)

    def close_driver(self):
        self.driver.close()

    def open_url(self, url):
        self.driver.get(url)

    def initialize_columns(self, columns):
        self.dataframe = pd.DataFrame(columns=columns)

    def set_credentials(self, email, password):
        self.credentials['email'] = email
        self.credentials['password'] = password

    def login(self):
        self.open_url('https://www.quora.com/')

        if (self.credentials['email'] and self.credentials['password']):

            email_element = self.driver.find_element(By.ID, 'email')
            password_element = self.driver.find_element(By.ID, 'password')
            email_element.send_keys(self.credentials['email'])
            password_element.send_keys(self.credentials['password'])
            # I tried adding a wait time but the script is not successful either way
            #self.driver.maximize_window()  # For maximizing window
            #self.driver.implicitly_wait(20)  # gives an implicit wait for 20 seconds
            # I tried clcking on Login through both Click and Enter but neither of them logs me in, even though I can see the button clicking
            password_element.send_keys(Keys.ENTER)
            e = self.driver.find_element(By.XPATH, "//div[text()='Login']")
            e.click()
        else:
            print('Credentials not set. Error')


if __name__ == "__main__":
    scraper = QuoraScraper()
    scraper.start_driver()
    email = "email"
    password = "password"
    scraper.set_credentials(email, password)
    scraper.login()

CodePudding user response:

I typed this //div[text()='Login'] into source code of quora site to check if this is the correct xpath for the login button, but instead it highlighted the Login text on top of the email, this is the correct xpath for the login button :

login_btn =  driver.find_element_by_xpath('//*[@id="root"]/div/div[2]/div/div/div/div/div/div[2]/div[2]/div[4]/button')

CodePudding user response:

Before you start with scripting. please understand the AUT. how exactly it works. using quora login page. as you enter the valid email address there is backend validation happening with server if the email is valid.

Unless and untill email address is validated and correct password the login button is disabled. Add an intermediate layer where check the attribute or wait for the attribute disabled=false. then proceed with click. this should solve the issue.

  • Related