如何在通过 Selenium 和 Python 自动化时使用 xpath/css 选择器在 drupal 8 网站中单击动态 link
How to click a dynamic link with in a drupal 8 website using xpath/css selector while automating through Selenium and Python
我正在尝试单击编辑选项卡 link,它的形式是无序列表中的超级link。
这里是 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
。
我也使用了 by partial text fxn,以及 html 的所有变体,并收到相同的错误。
还有下面的html我发现包括正确的link:
<link rel="edit-form" href="/node/2658/edit" />
我可以使用 selenium 函数转到此编辑页面吗?
根据 HTML,您共享了 href
和 data-drupal-link-system-path
属性的 value,由于存在值 2658
。所以需要构造一个动态定位器来定位元素。
由于所需的元素是一个动态元素,因此要在该元素上定位并 click()
您必须为 element_to_be_clickable()
引入 WebDriverWait 并且您可以使用以下任一项 :
使用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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
动态说明CSS_SELECTOR
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,您可以在 css-selectors 中使用以下通配符:
$ : 表示属性值 以
结尾
^ : 表示属性值 以
开头
所以最精细的 css_selector 将是:
li>a[href$='edit'][data-drupal-link-system-path^='node']
动态XPATH的解释
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,您可以使用 xpath 的以下函数:
contains()
:表示一个属性值包含
starts-with()
: 表示属性值 以
开头
所以最精细的 xpath 将是:
//li/a[contains(@href, 'edit') and starts-with(@data-drupal-link-system-path, 'node')][text()='Edit']
参考
您可以在以下位置找到一些相关讨论:
- Finding elements by CSS selector with ChromeDriver (Selenium) in Python
tl;博士
您可以在以下位置找到关于 NoSuchElementException 的详细讨论:
我正在尝试单击编辑选项卡 link,它的形式是无序列表中的超级link。
这里是 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
。
我也使用了 by partial text fxn,以及 html 的所有变体,并收到相同的错误。
还有下面的html我发现包括正确的link:
<link rel="edit-form" href="/node/2658/edit" />
我可以使用 selenium 函数转到此编辑页面吗?
根据 HTML,您共享了 href
和 data-drupal-link-system-path
属性的 value,由于存在值 2658
。所以需要构造一个动态定位器来定位元素。
由于所需的元素是一个动态元素,因此要在该元素上定位并 click()
您必须为 element_to_be_clickable()
引入 WebDriverWait 并且您可以使用以下任一项
使用
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()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC
动态说明CSS_SELECTOR
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,您可以在 css-selectors 中使用以下通配符:
$ : 表示属性值 以
结尾^ : 表示属性值 以
开头
所以最精细的 css_selector 将是:
li>a[href$='edit'][data-drupal-link-system-path^='node']
动态XPATH的解释
要仅考虑 href
和 data-drupal-link-system-path
属性的静态部分,您可以使用 xpath 的以下函数:
contains()
:表示一个属性值包含
开头starts-with()
: 表示属性值 以
所以最精细的 xpath 将是:
//li/a[contains(@href, 'edit') and starts-with(@data-drupal-link-system-path, 'node')][text()='Edit']
参考
您可以在以下位置找到一些相关讨论:
- Finding elements by CSS selector with ChromeDriver (Selenium) in Python
tl;博士
您可以在以下位置找到关于 NoSuchElementException 的详细讨论: