Home > database >  how to relogin and link to specified webpage with selenium python
how to relogin and link to specified webpage with selenium python

Time:02-20

I have two passwords A and B on the same website, sometimes login with password A and sometimes login with password B, whatever use password A or B, after login I need to link to one specified web page, but always stops after login and do not execute subsequent code. Below is my code, I tried to different functions but failed, I don't know what's problem with these code, please help, many thanks!

from selenium import webdriver
import time

driver = webdriver.Chrome()

def login():

    driver.get('website_url')

    while True:
        driver.find_element_by_xpath('//*[@id="Account"]').send_keys('Username')
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="Password"]').send_keys('Password A')
        time.sleep(2)
        driver.find_element_by_xpath('//*[@]').click()
        time.sleep(3)

        # if password A login failed, using password B

        try:
            strl = driver.find_element_by_xpath('//*[@]').text
        
            if strl == "Password Error":
                driver.find_element_by_xpath('//*[@id="Account"]').clear()
                time.sleep(2)
                driver.find_element_by_xpath('//*[@id="Account"]').send_keys('Username')
                time.sleep(2)
                driver.find_element_by_xpath('//*[@id="Password"]').send_keys('Password B')
                time.sleep(2)
                driver.find_element_by_xpath('//*[@]').click()
                time.sleep(3)

        except:
            print('Login Successfully!')
            return driver
login()

Always stop here and do not execute subsequent code

# This is where the main execution code starts.

def main():
    driver.get('webpage_url')

main()

CodePudding user response:

There are some mistakes that I see:

1- What is the reason of try-except? I cannot see any exception in the code.

2- You defined driver as global variable but you are returning it from the login function. So, this seems illogical. Also, you are not assigning this return value to any variable.

3- Usage of time.sleep() is a bad practice but I assume that these are only for debugging.

4- while True: statement creates an endless loop.

My advices:

1- You should remove try-except completely and only use if-else for this.

2- Remove return statement from the login function.

3- Define an entry point to your program such as if __name__ == '__main__':.

4- Remove while True: statement which creates an endless loop or use it carefully.

Putting all together:

from selenium import webdriver
import time

driver = webdriver.Chrome()

def login():

    driver.get('website_url')

    driver.find_element_by_xpath('//*[@id="Account"]').send_keys('Username')
    time.sleep(2)
    driver.find_element_by_xpath('//*[@id="Password"]').send_keys('Password A')
    time.sleep(2)
    driver.find_element_by_xpath('//*[@]').click()
    time.sleep(3)

    # if password A login failed, using password B

    strl = driver.find_element_by_xpath('//*[@]').text
        
    if strl == "Password Error":
        driver.find_element_by_xpath('//*[@id="Account"]').clear()
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="Account"]').send_keys('Username')
        time.sleep(2)
        driver.find_element_by_xpath('//*[@id="Password"]').send_keys('Password B')
        time.sleep(2)
        driver.find_element_by_xpath('//*[@]').click()
        time.sleep(3)

    print('Login Successfully!')


def main():

    login()
    driver.get('website_url')


if __name__ == '__main__':
    main()
  • Related