Home > other >  How to click on href link using Selenium and Python
How to click on href link using Selenium and Python

Time:08-11

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:

Code containing hyperlink

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!

CodePudding user response:

sorry I cannot comment due to my reputation.

I assume find_element does not return the correct element hence cannot click. Or you can verify it by changing .click() to .text. If it returns SITE MAP, then it returns a correct element.

I have another solution if you can try. It's using xpath and assumes class='pageheaderlink' is a single element in the HTML body.

list_subcontent = driver.find_elements_by_xpath(".//span[@class='pageheaderlink']")

for item in list_subcontent:
    if item.text == 'SITE MAP':
        item.click()

help to upvote if this helps

CodePudding user response:

Considering the HTML:

element

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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.PARTIAL_LINK_TEXT, "SITE MAP"))).click()
    
  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "a.submenulinktext2"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//a[@class='submenulinktext2' and contains(., 'SITE MAP')]"))).click()
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • Related