I am working on a program that automates logs into to a certain webpage and clicks certain buttons & links to reach a final destination to enter certain values and submit them. I have managed to navigate through the webpages but one of the webpages has a hyperlink button that I need Selenium to click, however, after trying multiple different methods, I cannot get it to work.
I have tried finding the element with By.XPATH
, By.LINK_TEXT
, By.PARTIAL_LINK_TEXT
and none of these worked. I thought my issue might be that since it is clicking onto a totally new URL, so I load the new URL towards the bottom of my code to then move forward with my program.
The hyperlink button: Button
The chunk of code to the hyperlink button I am trying to click on:
The XPath itself is : /html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]
driver = webdriver.Chrome(executable_path='C:\chromedriver.exe')
driver.get('')
'''
username_input = '//*[@id="userNameInput"]'
password_input = '//*[@id="passwordInput"]'
submit_button = '//*[@id="submitButton"]'
send_push = '//*[@id="auth_methods"]/fieldset/div[1]/button'
'''
# enters username and password into fields
driver.find_element("xpath", '//*[@id="userNameInput"]').click()
driver.find_element("xpath", '//*[@id="userNameInput"]').send_keys(username)
driver.find_element("xpath", '//*[@id="passwordInput"]').click()
driver.find_element("xpath", '//*[@id="passwordInput"]').send_keys(password)
driver.find_element("xpath", '//*[@id="submitButton"]').click()
# clicks 'send me a push' button on duo mobile screen
WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='duo_iframe']")))
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", "//button[normalize-space()='Send Me a Push']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable(("xpath", '//*[@id="p_p_id_56_INSTANCE_xWhKj4tIFYvm_"]/div/div/div[1]/a[5]'))).click()
# loads next url which has the link on its webpage that needs to be clicked
driver.get('')
# attempts to click on link
driver.find_element("xpath", '/html/body/div[2]/table/tbody/tr/td[2]/p/span/a[2]').click()
I have removed the URLs in driver.get('')
as they contain sensitive URLs
My last line of code is my attempt to click the hyperlink using the XPath
Any help is appreciated!
EDIT:
After further investigation, I now see that 2 buttons both have the same class name in their elements:
The SITE MAP button: SITE MAP
The Student button: Student
Considering the HTML:
The element with text as SITE MAP is a
<a>
tag and a dynamic element. To click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:Using PARTIAL_LINK_TEXT:
Using CSS_SELECTOR:
Using XPATH:
Note: You have to add the following imports :