如何在通过硒和Python自动执行的同时使用xpath/css选择器在drupal 8网站中单击动态链接 [英] How to click a dynamic link with in a drupal 8 website using xpath/css selector while automating through Selenium and Python
问题描述
我试图单击编辑"选项卡链接,其形式为无序列表中的超链接.
I am trying to click on an edit tab link, and its in the form of a hyperlink in an unordered list.
这是HTML:
<li><a href="/node/2658/edit" data-drupal-link-system-path="node/2658/edit">Edit</a></li>
我一直在尝试使用 driver.find_element_by_link_text('Edit')
查找元素,但是始终会得到 NoSuchElementException
.
I have been trying to use driver.find_element_by_link_text('Edit')
to find the element, however always get a NoSuchElementException
.
我还使用了部分文本fxn和html的所有变体,并且收到相同的错误.
I have also used the by partial text fxn, with all variations of the html, and receive the same error.
我还发现了以下包含正确链接的html:
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?
推荐答案
按照HTML,您共享了 href
和 data-drupal-link的 value 由于存在值
属性显然是动态的.因此,您需要构造一个动态定位器来定位元素. 2658
,-system-path
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.
由于所需元素是动态元素,因此要定位并在该元素上 click()
,您必须为 element_to_be_clickable()引入 WebDriverWait 代码>,您可以使用以下任一定位器策略:
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:
-
使用
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()
使用 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
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,可以在 css-selectors :
To consider only the static part of href
and data-drupal-link-system-path
attributes you can use the following wildcards in css-selectors:
-
$ :表示属性值结尾
^ :表示属性值起始于
因此,最精细的css_selector将是:
So the most granular css_selector would be :
li>a[href$='edit'][data-drupal-link-system-path^='node']
动态XPATH的解释
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,可以使用 xpath :
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()
:表示属性值包含
starts-with()
:表示属性值以
因此,最精细的xpath将是:
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:
- Java硒webdriver表达式通过ccs查找动态元素,以 开头和结尾
- 如何在Python中使用Selenium来获取内部具有动态部分的选择器?
- 通过CSS选择器查找元素在Python中使用ChromeDriver(硒)
您可以在以下位置找到有关 NoSuchElementException 的详细讨论:
You can find a detailed discussion on NoSuchElementException in:
这篇关于如何在通过硒和Python自动执行的同时使用xpath/css选择器在drupal 8网站中单击动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!