Search code examples
pythonseleniumselenium-webdriverbasic-authentication

After successful web login with selenium, can not use newly loaded page


I wrote a script that successfully logs into a website. However, after I am logged in the page changes but the URL stays the same. It does not redirect to a different URL that I can now use to find elements and fill forms.

Here is the script:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException


driver = webdriver.Firefox()
driver.implicitly_wait(10)
base_url = "https://trakcarelabwebview.nhls.ac.za/trakcarelab/csp/logon.csp"
verificationErrors = []
driver.get(base_url)

##log into website by filling in username and password forms
driver.find_element_by_id("USERNAME").send_keys("XXXX")
driver.find_element_by_id("PASSWORD").send_keys("XXXX")
driver.find_element_by_class_name("clsButton").click()


## at this point the page chages and I am unable to find any of the new elements on the new page

barcode = "11111111"
driver.implicitly_wait(5)
##driver.find_element_by_id("Clear").click()
driver.find_element_by_id("SpecimenParam").send_keys(barcode)
driver.find_element_by_id("Find").click()

driver.quit()

I am not sure at all how I can get selenium to use the newly loaded page after login as the new page on which operations will be performed.


Solution

  • Selenium doesn't care about page or url changes. It only looks in the browser instance created by driver and whatever rendered in that browser instance.

    You have to see if the web elements you are searching is accessible using different locator strategies (id, class name, link text, xpath, css selector etc).

    If the webpage is loading slow, use explicit waits on the element:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
    from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
    
    ff = webdriver.Firefox()
    ff.get("http://somedomain/url_that_delays_loading")
    try:
        element = WebDriverWait(ff, 10).until(EC.presence_of_element_located((By.ID, "myDynamicElement")))
    finally:
        ff.quit()
    

    If still the element is not found but your locators are correct, then there is high chance that, it might be rendered in an IFrame, for which you have to switch to that frame before finding element.

    Refer this link for switching to an IFrame: selecting an iframe using python selenium