通过多个类名查找div元素? [英] Find div element by multiple class names?

查看:36
本文介绍了通过多个类名查找div元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想识别那个网络元素.它只定义了这两个类.我不能执行以下操作,因为 className 不采用空格分隔值.什么是替代方案?

@FindBy(className = "value test")@CacheLookup私人 WebElement 测试;

解决方案

我认为 barak manos 的回答没有完全解释它.

假设我们有以下几个元素:

  1. <div class="value test "></div>
  2. <div class="first value test last"></div>

XPath 如何匹配

  • 只匹配 1 个(完全匹配),巴拉克的答案

    driver.findElement(By.xpath("//div[@class='value test']"));

  • 匹配 1、2 和 3(匹配类包含 value test,类顺序很重要)

    driver.findElement(By.xpath("//div[contains(@class, 'value test')]"));

  • 匹配 1、2、3 和 4(只要元素具有类 valuetest)

    driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"));

此外,在这种情况下,Css Selector 总是支持 XPath(快速、简洁、原生).

  • 匹配 1

    driver.findElement(By.cssSelector("div[class='value test']"));

  • 匹配 1、2 和 3

    driver.findElement(By.cssSelector("div[class*='value test']"));

  • 匹配 1、2、3 和 4

    driver.findElement(By.cssSelector("div.value.test"));

<div class="value test" /> I'd like to identify that web element. It only has this two classes defined. I cannot do the following as className does not take a space separated value. What are alternatives?

@FindBy(className = "value test")
@CacheLookup
private WebElement test;

解决方案

I don't think barak manos's answer has fully explained it.

Imagine we have few elements as the followings:

  1. <div class="value test"></div>
  2. <div class="value test "></div>
  3. <div class="first value test last"></div>
  4. <div class="test value"></div>

How XPath matches

  • Match only 1 (exact match), barak's answer

    driver.findElement(By.xpath("//div[@class='value test']"));
    

  • Match 1, 2 and 3 (match class contains value test, class order matters)

    driver.findElement(By.xpath("//div[contains(@class, 'value test')]"));
    

  • Match 1, 2, 3 and 4 (as long as elements have class value and test)

    driver.findElement(By.xpath("//div[contains(@class, 'value') and contains(@class, 'test')]"));
    

Also, in cases like this, Css Selector is always in favor of XPath (fast, concise, native).

  • Match 1

    driver.findElement(By.cssSelector("div[class='value test']"));
    

  • Match 1, 2 and 3

    driver.findElement(By.cssSelector("div[class*='value test']"));
    

  • Match 1, 2, 3 and 4

    driver.findElement(By.cssSelector("div.value.test"));
    

这篇关于通过多个类名查找div元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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