java 如何通过除“class”和“name”(例如“title”)之外的属性直接查找WebElements

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32236054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 19:52:13  来源:igfitidea点击:

How to directly find WebElements by their attributes except "class" and "name" (for example "title")

javaseleniumtestingselenium-webdriverselenium-chromedriver

提问by LoveLovelyJava

I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.

我是 Java 和 Selenium 的新手,所以如果我的问题听起来有点初级,我提前道歉。

I use:

我用:

driverChrome.findElements(By.className("blabla"));

to find elements which have "blabla" as their className, for example:

查找以“blabla”作为类名的元素,例如:

<span class="blabla" title="the title">...</span>

Now, what if I want to find all elements by their other attributes? something like:

现在,如果我想通过其他属性查找所有元素怎么办?就像是:

driverChrome.findElements(By.titleValue("the title"));

This is the code that I am currently using to do this task:

这是我目前用于执行此任务的代码:

List<WebElement> spans = driverChrome.findElements(By.tagName("span"));

for (WebElement we : spans) {

    if (we.getAttribute("title") != null) {
            if (we.getAttribute("title").equals("the title")) {
                    ...
                    break;
            }
    }

}

but it is not fast and easy to use.

但它并不快速且易于使用。

回答by J.Lyu

There are many methods while archiving an element by XPath

通过XPath归档元素时有很多方法

1 Absolutely path

1 绝对路径

<html>
  <body>
     <div>
       <form>
          <input id="demo"/>
       </form>
     </div>
   </body>
 <html>

To get the 'input' tag

获取“输入”标签

xpath="/html/body/div/form/input"

2 Relative path

2 相对路径

<html>
  <body>
     <div>
       <form>
          <input id="demo"/>
       </form>
     </div>
   </body>
 <html>

To get the 'input' tag

获取“输入”标签

xpath="//input"  

3 Index

3 索引

<html>
  <body>
     <div>
       <form>
          <input id="demo1"/>
          <input id="demo2"> 
       </form>
     </div>
   </body>
 <html>

To get the input 'demo2'

获取输入'demo2'

xpath="//input[1]"

xpath="//输入[1]"

4 Arbitrary single attribute

4 任意单属性

<html>
  <body>
     <div>
       <form>
          <input id="demo1"/>
          <input id="demo2" foo="bar"> 
       </form>
     </div>
   </body>
 <html>

To get input 'demo2'

获取输入'demo2'

xpath="//input[@id='demo2']" (equivalent to By.id)

Or

或者

xpath="//input[@foo='bar']"

5 Arbitrary multiple attributes

5 任意多个属性

<html>
    <body>
     <div>
       <form>
          <input id="1" type="submit" />
          <input id="2" foo="bar"/>
          <input id="3" type="submit" foo="bar"/> 
       </form>
     </div>
   </body>
 <html>

To get 3rd input

获得第三个输入

xpath="//input[@type='submit'][@foo='bar']"

Or

或者

xpath="//input[@type='submit' and @foo='bar']"

If use xpath="//input[@type='submit' or @foo='bar']" here you'll get an array. You can get the List by driver.findElements(By.xpath(xpath)) (java). Otherwise you'll get the first element(If you just use driver.findElement). Because all of the 3 input elements meet your condition 'or' and it gives you the first one.

如果使用 xpath="//input[@type='submit' or @foo='bar']" 在这里你会得到一个数组。您可以通过 driver.findElements(By.xpath(xpath)) (java) 获取列表。否则你会得到第一个元素(如果你只使用 driver.findElement)。因为所有 3 个输入元素都满足您的条件 'or' 并且它为您提供了第一个。

6 Contains attribute

6 包含属性

<html>
    <body>
     <div>
       <form>
          <input id="1" type="submit" />
          <input id="2" foo="bar" daddy="dog"/>
          <input id="3" type="submit" foo="bar"/> 
       </form>
     </div>
   </body>
 <html>

To get the second input

获取第二个输入

xpath="//input[@daddy]"

Because only the second one has attribute 'daddy'

因为只有第二个有属性 'daddy'

7 Inner searching

7 内部搜索

 <html>
    <body>
     <div>
       <form>
          <input id="input1" daddy="dog" />
          <input id="input2" daddy="pig"/>
       </form>
     </div>
     <div>
       <form>
          <input id="input3" daddy="dog" />
          <input id="input4" daddy="apple"/>
       </form>
     </div>
   </body>
 <html>

To get the second div

获取第二个 div

xpath="//div[.//input[@daddy='dog'] and .//input[@daddy='apple']]"

Overall those are all I can work out for now. Hope it helps.

总的来说,这些就是我现在可以解决的。希望能帮助到你。

回答by Pablo Miranda

You can do it using xpath

你可以使用 xpath

driverChrome.findElement(By.xpath("//*[@yourAttribute='value']"));

For example:

例如:

driverChrome.findElement(By.xpath("//*[@title='the title']"));

回答by Alex Collins

You can also achieve this using CSS locators.

您也可以使用 CSS 定位器来实现这一点。

By.cssSelector ("span[title='the title']")

By.cssSelector("span[title='the title']")

CSS selector are rumoured to be faster.

传闻 CSS 选择器更快。

We cover a number of locating techniques in Selenium WebDriver in Practice: http://selenium-webdriver-in-practice.github.io

我们在实践中介绍了 Selenium WebDriver 中的许多定位技术:http: //selenium-webdriver-in-practice.github.io

回答by Yu Zhang

There a number of options you can go for.

您可以选择多种选择。

  1. by css selector
  2. by xpath
  3. by id
  4. by tag For easy reading tutorials, please follow the links below: http://www.w3schools.com/xsl/xpath_syntax.asphttp://www.w3schools.com/cssref/css_selectors.asp
  1. 通过 css 选择器
  2. 通过 xpath
  3. 按身
  4. 按标签为便于阅读教程,请点击以下链接: http://www.w3schools.com/xsl/xpath_syntax.asp http://www.w3schools.com/cssref/css_selectors.asp

All above is using element attributes, alternatively, you can use relationship among elements. Such as sibling, parents and children.

以上都是使用元素属性,或者,你可以使用元素之间的关系。比如兄弟姐妹,父母和孩子。

Please refer to this awesome link for more details. http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf

请参阅这个很棒的链接以获取更多详细信息。http://scraping.pro/res/xpath-cheat/xpath_css_dom_recipes.pdf

There are also very well supported and established frameworks for you to use. For example, robot framework, it will vastly simplify your codes.

也有很好的支持和建立的框架供您使用。例如机器人框架,它将极大地简化您的代码。

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html

http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html

回答by dev

 WebElement element = driver.findElement(By.xpath(".//*[@id='ctl00_PLSMainContent_AssistTaskList_assistListView']/div/table/tbody/tr/td[7]/span"));

 System.out.println("get attribute is --> " + element.getAttribute("Title"));