Java Selenium webdriver表达式通过ccs查找以开头和结尾

Java Selenium webdriver expression finding dynamic element by ccs that starts with and ends with(Java Selenium webdriver表达式通过ccs查找以开头和结尾的动态元素)
本文介绍了Java Selenium webdriver表达式通过ccs查找以开头和结尾的动态元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要找到以下 HTML 元素.现在,这个id"name 是动态的,就像 int 0";最后会改变,但我知道它会是什么.0-0"中的第一个 int也会改变,但会是什么都无所谓.

我尝试了下面的代码来查找以#ui-select-choices-row-"开头的元素.并以所需的int"输入结束,但没有按预期找到它.关于我在这里做错了什么有什么建议吗?

尝试 1:

driver.findElement(By.cssSelector("div[id^='#ui-select-choices-row-'] 和 div[id$='"+int+"div']"));

尝试 2:

driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'] and div[id$='"+int+"']"));

解决方案

你已经足够接近了.您需要将 id 开头的 # 删除为 # 本身表示 id 属性.


动态 CSS_SELECTOR 的解释

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

  • $ :表示一个属性值

    结尾
  • ^ : 表示一个属性值

    开头

所以最细粒度的 css_selector 应该是:

div[id^='ui-select-choices-row-'][id$='"+count+ "']


解决方案

您的有效代码行将是:

driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'][id$='"+count+"']"));

注意:int为关键字,变量名使用其他名称.


参考文献

您可以在以下位置找到一些相关的详细讨论:

  • 通过 CSS 选择器查找元素在 Python 中使用 ChromeDriver (Selenium)
  • 如何在通过 Selenium 和 Python 实现自动化的同时使用 xpath/css 选择器在 drupal 8 网站中单击动态链接
  • 如何使用 Selenium 和 Python 获取内部具有动态部分的选择器?

I have the below HTML element that I need to find. Now, this "id" name is dynamic in the way that the int "0" at the end will change, but I know what it will be. The first int in "0-0" will also change, but it doesn't matter what it will be.

<div id="ui-select-choices-row-0-0">

I've tried the below code that looks for an element that starts with "#ui-select-choices-row-" and ends with the desired input of "int", but it's not finding it as expected. Any suggestions on what I'm doing wrong here?

Attempt 1:

driver.findElement(By.cssSelector("div[id^='#ui-select-choices-row-'] and div[id$='"+int+" div']"));

Attempt 2:

driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'] and div[id$='"+int+"']"));

解决方案

You were close enough. You need to remove the # from the beginning of the id as # itself denotes the id attribute.


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 :

div[id^='ui-select-choices-row-'][id$='" +count+ "']


Solution

Your effective line of code will be:

driver.findElement(By.cssSelector("div[id^='ui-select-choices-row-'][id$='" +count+ "']"));

Note: int is a keyword, use other names as the variable name.


References

You can find a couple of relevant detailed discussions in:

  • Finding elements by CSS selector with ChromeDriver (Selenium) in Python
  • How to click a dynamic link with in a drupal 8 website using xpath/css selector while automating through Selenium and Python
  • How to get selectors with dynamic part inside using Selenium with Python?

这篇关于Java Selenium webdriver表达式通过ccs查找以开头和结尾的动态元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How can create a producer using Spring Cloud Kafka Stream 3.1(如何使用Spring Cloud Kafka Stream 3.1创建制片人)
Insert a position in a linked list Java(在链接列表中插入位置Java)
Did I write this constructor properly?(我是否正确地编写了这个构造函数?)
Head value set to null but tail value still gets displayed(Head值设置为空,但仍显示Tail值)
printing nodes from a singly-linked list(打印单链接列表中的节点)
Control namespace prefixes in web services?(控制Web服务中的命名空间前缀?)