1

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>
DV123
  • 11
  • 3
  • As per the details provided, the `LINK_TEXT` is `Import Offer Activity`. Try the full text or try with `partial_link _text`. – pmadhu Sep 17 '21 at 15:37
  • Sorry, that was a typo. The link name I added in the code is Import Offer activity. – DV123 Sep 17 '21 at 16:18

1 Answers1

0

change

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

to

driver.find_element(By.XPATH, "//a[contains(text(), 'Offer Activity')]").click()
ljlozano
  • 181
  • 10
  • That did not work too, It works if I hard code the user name and password but when I use data-driven just for user name and password and then try to click the link it doesn't. If you have any other ideas let me know. Thank You – DV123 Sep 17 '21 at 16:34
  • If it works when you're hard-coding the username and password, it sounds like you need to ensure you're getting the correct data from `username = XLutils.readData(path, "Login", 2, 1)` as well as `password = XLutils.readData(path, "Login", 2, 2)` -- Do you notice the correct information (username, password) being sent to the screen before it clicks? If you can't tell, add a `driver.explicit_wait(10)` before you click to check the data being sent. – ljlozano Sep 17 '21 at 17:24
  • yes, I see the right information being sent and log in to the application with that data. The link for the report I am trying to open (offer activity) is inside the application and that is where it stops. – DV123 Sep 17 '21 at 18:00
  • Could you post the output of the error you're getting? – ljlozano Sep 17 '21 at 18:17
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":" Offer Activity"} (Session info: chrome=93.0.4577.63) – DV123 Sep 17 '21 at 18:28
  • That is the error you get after changing the code I noted originally? – ljlozano Sep 17 '21 at 18:33
  • selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//a[contains(text(), 'Offer Activity')]"} – DV123 Sep 20 '21 at 14:53