DV123
DV123

Reputation: 11

Unable to select the link with link_text in selenium with python

I am new to automation and trying to automate a website with links in it. When I tried hardcoding the user name and password and then selecting the link using Link_text, the code worked, But When I tried fetching data from Excel file for user name and password, the user name and password field works but the link is not getting selected. Can someone please help?

This is the code

    import XLutils
    from selenium import webdriver
    
    from selenium.webdriver.common.by import By
    from selenium.webdriver.common.keys import Keys
    
    driver = webdriver.Chrome(r'C:\Users\test\AppData\Local\Programs\chromedriver_win32\chromedriver.exe')
    
    driver.get("https://test.com")
    
    driver.maximize_window()
    
    path = r'C:\Users\test\Desktop\Reports Test.xlsx'
    
    rows = XLutils.getRowCount(path, 'Login')
    
    username = XLutils.readData(path, "Login", 2, 1)
    password = XLutils.readData(path, "Login", 2, 2)
    
    driver.find_element(By.ID, 'username').clear()
    driver.find_element(By.ID, 'username').send_keys(username)
    driver.find_element(By.ID, 'password').clear()
    driver.find_element(By.ID, 'password').send_keys(password)
    driver.find_element(By.XPATH, '/html/body/app-root/body/div/app-login/div\[2\]/div\[2\]/form/div\[4\]/button').click()
    
    driver.find_element(By.LINK_TEXT, 'Offer Activity').click()

html

<a class="reportLink" href="null"> Offer Activity </a>

Upvotes: 1

Views: 1273

Answers (1)

ljlozano
ljlozano

Reputation: 191

change

driver.find_element(By.LINK_TEXT, 'Offer Activity').click()

to

driver.find_element(By.XPATH, "//a[contains(text(), 'Offer Activity')]").click()

Upvotes: 0

Related Questions