1
votes

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?

1
@KunduK Could you expand on that? Not familiar with what you're talking about - Michael Lively
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
No worries.If you inspect your element the EDIT element just traverse upwards in the DOM tree and check if there any tag <iframe> if there you need to switch to that iframe in oder to access. - KunduK
Viewing the entire page source, there are no instances of <iframe> anywhere . Well... there are a couple, but none none in any relevant sections and they're always closed in the same line - Michael Lively
Well the iframe tag always be start and close. However Check the answer posted by @DebanjanB Hope this will help to your resolution. - KunduK

1 Answers

0
votes

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: