如何在通过 Selenium 和 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

查看:28
本文介绍了如何在通过 Selenium 和 Python 自动化时使用 xpath/css 选择器在 drupal 8 网站中单击动态链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试单击一个编辑选项卡链接,它的形式是无序列表中的超链接.

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.

我还使用了带有所有 html 变体的 by partial text fxn,并收到相同的错误.

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" />

我可以使用 selenium 函数转到此编辑页面吗?

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

推荐答案

根据您共享的 HTML,hrefdata-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
    

  • 要仅考虑 hrefdata-drupal-link-system-path 属性的静态部分,您可以在 :

    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 的解释

    要仅考虑 hrefdata-drupal-link-system-path 属性的静态部分,您可以使用 :


    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() : 表示一个属性值contains

    starts-with() : 表示一个属性值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:

    您可以在以下位置找到关于 NoSuchElementException 的详细讨论:

    You can find a detailed discussion on NoSuchElementException in:

    这篇关于如何在通过 Selenium 和 Python 自动化时使用 xpath/css 选择器在 drupal 8 网站中单击动态链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆