Script for link redirects

Question:

I am trying to write a python script, using selenium, to go into a website (Salesforce.com) and change various input fields and take screenshots. However, I am running into issues when it comes to my organization’s SSO interface. My URL redirects me first, to a sign-on page (which asks for org email), followed by a page that requires login credentials before I can access the dashboard I am looking for.

For the purpose of this dicsussion, I will call the sign-on page, page #1, and the login-in redirect page, page #2.

My selenium script is able to click on element in page #1 but is unable to do anything on page #2. I have already experimented with explicit and implicit waits. But I think the issue has to do with the redirect, which occurs in the same window/tab, and not a ‘waiting’ period for the page to load.

Here is my code:

url = someurl.com
class MyTest(object): 
    def __init__(self): 
        self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()))
        
    def login_process(self): 
        self.driver.get(url) 
        element = self.driver.find_element(By.ID, 'idp-discovery-username') 
        element.clear() 
        element.send_keys("[email protected]") 
        self.driver.find_element(By.ID, 'idp-discovery-submit').submit() 
        self.driver.implicitly_wait(10)
        # login_button = WebDriverWait(self.driver, 20).until(
        #         EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='IFrame_htmIFrame']"))
        #         )

    def query(self): 
        self.driver.find_element(By.XPATH, '/html/body/div/div[2]/div[2]/article/form/input[6]')

test = MyTest()
test.login_process()
test.query()

I will also link inspected elements of both pages to help find a solution.

Thanks in advance.

Page #1, inspected

Page #2, inspected

In the second image, the green box is what I am trying to access with:

self.driver.find_element(By.XPATH, '/html/body/div/div[2]/div[2]/article/form/input[6]')

Asked By: raoyourboat

||

Answers:

Try adding self.driver.implicitly_wait(10) to the end of the __init__ method.
Or add an explicit wait into query method:

def query(self):
    WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, 'login-button'))).click()
Answered By: Eugeny Okulik

problematic code:

self.driver.implicitly_wait(10)
# login_button = WebDriverWait(self.driver, 20).until(
#         EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='IFrame_htmIFrame']"))
#         )

An explicit wait (similar to that which was commented out) seems to work better here.

I changed

login_button = WebDriverWait(self.driver, 20).until(
    EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, "iframe[id$='IFrame_htmIFrame']")))

to

login_button = WebDriverWait(self.driver, 20).until(
     EC.element_to_be_clickable((By.ID, 'login-button')))
login_button.click()

and it has worked. Thanks all and hope this helps someone else.

Answered By: raoyourboat
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.