问题
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:
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 css-selectors:
$ : 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 xpath:
contains()
: To indicate an attribute value containsstarts-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:
- How to find element by part of its id name in selenium with python
- Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with
- How to get selectors with dynamic part inside using Selenium with Python?
- Finding elements by CSS selector with ChromeDriver (Selenium) in Python
tl; dr
You can find a detailed discussion on NoSuchElementException in:
- Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome
来源:https://stackoverflow.com/questions/59777770/how-to-click-a-dynamic-link-with-in-a-drupal-8-website-using-xpath-css-selector