Home > Blockchain >  Why Selenium method get(), doesnt work second time?
Why Selenium method get(), doesnt work second time?

Time:10-08

Help me figure out why when using the get () method a second time, the page does not go? The method works only if you use a time delay time.sleep ()

Not working:

LOGIN = '[email protected]'
PASS = 'somepass'
LINK = 'https://stepik.org/'

browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[@type = "submit"]').click()
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')

Working

LOGIN = '[email protected]'
PASS = 'somepass'
LINK = 'https://stepik.org/'

browser = webdriver.Chrome()
browser.get(LINK)
browser.implicitly_wait(5)
browser.find_element_by_id('ember232').click()
username = browser.find_element_by_name('login').send_keys(LOGIN)
pas = browser.find_element_by_name('password').send_keys(PASS)
button = browser.find_element_by_xpath('//button[@type = "submit"]').click()
time.sleep(5)
browser.get('https://stepik.org/lesson/237240/step/3?unit=209628')

CodePudding user response:

Try an alternative way:

driver.navigate().to("https://stepik.org/lesson/237240/step/3?unit=209628")

CodePudding user response:

You are trying to login into the web site and then to navigate to some internal page.
By clicking the submit button

button = browser.find_element_by_xpath('//button[@type = "submit"]').click()

You are trying to log into the site.
This process takes some time.
So if immediately after clicking the submit page, while login is still not proceed, you are trying to navigate to some internal page this will not work since you still not logged in.
However you do not need to use a hardcoded sleep of 5 seconds.
You can use an explicit wait of expected conditions like presence_of_element_located() of some internal element to indicate you are inside the web site. Once this condition is fulfilled you can navigate to the desired internal page.

  • Related