0

When I am trying to click the Incident tab as highlighted in Image, it gives me an error. Below is the script.

from selenium import webdriver
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("https://*****.service-now.com/")
driver.switch_to.frame('gsft_main')
driver.find_element_by_id('user_name').send_keys('*****')
driver.find_element_by_id('user_password').send_keys('*****')
driver.find_element_by_id('sysverb_login').click()
Incident=driver.find_element_by_link_text('Incident')

This is the element I got when copied after Inspecting the page

<span>Incident</span>

This is xpath

//*[@id="concourse_application_dead1309c611228701e2bda7b4252474"]/a/span

Tried both but no success. Gives me below error.`

Traceback (most recent call last):    
  File "Test1.py", line 10, in <module>    
    Incident=driver.find_element_by_link_text('Incident')    
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 428, in find_element_by_link_text
    return self.find_element(by=By.LINK_TEXT, value=link_text)    
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element 'value': value})['value']    
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
self.error_handler.check_response(response)    
  File "C:\Python37\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response    
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"link text","selector":"Incident"}
Guy
  • 46,488
  • 10
  • 44
  • 88
Pawan
  • 1
  • 2

2 Answers2

0

find_element_by_link_text() and find_element_by_partial_link_text() works only on <a> tags, your element is a <span>. You need to use xpath for to locate it

incident = driver.find_element_by_xpath('//span[contains(., "Incident")]')

Since you are navigated to another page you should also add some wait

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

wait = WebDriverWait(driver, 10)
incident = wait.until(ec.visibility_of_element_located((By.XPATH, '//span[contains(., "Incident")]')))
Guy
  • 46,488
  • 10
  • 44
  • 88
0

The element with text as Incident is actually within a <span> node. Though it is within a parent <a> node, you won't be able to use find_element_by_link_text() or find_element_by_partial_link_text() to locate the element.


However, as the desired element is a dynamic element so to locate and click() on the element you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[id^='concourse_application_dead']>a>span")))
    
  • Using XPATH A:

    incident = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Incident']")))
    
  • 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
    

Reference

You can find a detailed relevant discussion in:

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352