1

I am trying to click on an edit tab link, and its in the form of a hyperlink in an unordered list.

Here is the HTML:

<li><a href="/node/2658/edit" data-drupal-link-system-path="node/2658/edit">Edit</a></li>

I have been trying to use driver.find_element_by_link_text('Edit') to find the element, however always get a NoSuchElementException.

I have also used the by partial text fxn, with all variations of the html, and receive the same error.

There is also the following html I found that includes the proper link:

<link rel="edit-form" href="/node/2658/edit" />

Is there a selenium function I can use to go to this edit page?

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • @KunduK Could you expand on that? Not familiar with what you're talking about – Michael Lively Jan 16 '20 at 21:22
  • I wish I could, however it requires being logged into the dev mode of our website, so unfortunately I can't share that info... – Michael Lively Jan 16 '20 at 21:27
  • No worries.If you inspect your element the EDIT element just traverse upwards in the DOM tree and check if there any tag ` – KunduK Jan 16 '20 at 21:30
  • Viewing the entire page source, there are no instances of – Michael Lively Jan 16 '20 at 21:33
  • Well the iframe tag always be start and close. However Check the answer posted by @DebanjanB Hope this will help to your resolution. – KunduK Jan 16 '20 at 21:35

1 Answers1

0

As per the HTML you shared the value of href and data-drupal-link-system-path attributes clearly seems to be dynamic due to the presence of the value 2658. So you need to construct a dynamic locator to locate the element.

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:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "li>a[href$='edit'][data-drupal-link-system-path^='node']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li/a[contains(@href, 'edit') and starts-with(@data-drupal-link-system-path, 'node')][text()='Edit']"))).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
    

Explanation of the dynamic CSS_SELECTOR

To consider only the static part of href and data-drupal-link-system-path attributes you can use the following wildcards in :

  • $ : To indicate an attribute value ends with

  • ^ : To indicate an attribute value starts with

So the most granular css_selector would be :

li>a[href$='edit'][data-drupal-link-system-path^='node']

Explanation of the dynamic XPATH

To consider only the static part of href and data-drupal-link-system-path attributes you can use the following functions of :

  • contains() : To indicate an attribute value contains

  • starts-with() : To indicate an attribute value starts with

So the most granular xpath would be :

//li/a[contains(@href, 'edit') and starts-with(@data-drupal-link-system-path, 'node')][text()='Edit']

Reference

You can find a couple of relevant discussions in:


tl; dr

You can find a detailed discussion on NoSuchElementException in:

DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • You sir, are a life saver. The CSS option worked perfectly. Could you explain how you came to that conclusion so quickly, I'd like to understand on a deeper level – Michael Lively Jan 16 '20 at 21:38
  • @MichaelLively Updated the answer with all the relevant details and references for your deeper level understanding. Let me know if you have further queries. – DebanjanB Jan 17 '20 at 07:42