Selenium Unable to locate Element (Partial Text Link)

Viewed 400

I am new to Selenium and I try to click a link on a second page (after the login page). The login part works and the browser goes to the next page however on the next page it raises an exception:

This is my Traceback:

Traceback (most recent call last):
  File "d:/Personal_projects/Programmeren/Python/Automate the boring stuff/Chapter12/seleniumTest.py", line 22, in <module>
    weekElem = browser.find_element(By.PARTIAL_LINK_TEXT,'/go/')
  File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 1246, in find_element
    'value': value})['value']
  File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\webdriver.py", line 424, in execute
    self.error_handler.check_response(response)
  File "C:\Users\****\AppData\Roaming\Python\Python37\site-packages\selenium\webdriver\remote\errorhandler.py", line 247, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"partial link text","selector":"/go/"}
  (Session info: chrome=96.0.4664.93)

This is my code (login info obviously blurred):

#! python3

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

import time
browser = webdriver.Chrome()

# First webpage
browser.get('https://www.signupgenius.com/register')
userElem = browser.find_element(By.ID,'email')
userElem.send_keys('************')
passwordElem = browser.find_element(By.ID,'pword')
passwordElem.send_keys('**********')
loginBtnElem = browser.find_element(By.ID,'loginBtnId')
loginBtnElem.click()
time.sleep(10)

# Second  webpage
weekElem = browser.find_element(By.PARTIAL_LINK_TEXT,'/go/')
weekElem.click()

The element that I am looking for is:

<a href="/go/10C426245672F85" class="text-orange text-underlined"><strong class="ng-binding">1212</strong></a>

(the part after /go/ changes every week, that why I use Partial Link Text)

I hope someone could help me out with this one!

EDIT I found the fix, by using Xpath the problem was solved:

weekElem = browser.find_element(By.XPATH, '//a[contains(@href,"/go/")]')

If someone could elaborate WHY partial link text does not work, that would give me more insight in how Selenium works!

1 Answers

The string go isn't part of the LINK_TEXT of the A tag. Instead it's the part of the value of the href attribute. Hence you can't use go as a PARTIAL_LINK_TEXT.


Solution

You can use the string go as a part of the value of the href attribute as follows:

  • Using starts-with():

    weekElem = browser.find_element(By.XPATH, '//a[starts-with(@href,"/go/")]/strong')
    
  • Using contains():

    weekElem = browser.find_element(By.XPATH, '//a[contains(@href,"/go/")]/strong')
    
Related