通常selenium By 可以通过id,class,xpath,css等定位页面元素。

id 和class比较简单,此处略....

 

介绍下xpath的常用内置函数 contains()/not (contains()), starts-with()/ends-with(), position(), last()...

代码如下

    @FindBy(how = How.XPATH, using = "//span[starts-with(@id,'bn_id')]/span[contains(@class,'bn_class')]")
    private List<WebElement> bnSpan;
    @FindBy(how = How.XPATH, using = "//div[contains(text(),'report')]") 
    private WebElement reportXpath;

 

@FindBy(how = How.XPATH, using = "//span[not(contains(@class,'bn_classB'))]")
    private List<WebElement> bnXpath;

position()--指定该div的排位

    @FindBy(how = How.XPATH, using = "//li[@id='li_id']/div[position()=1]")
    private List<WebElement> firstDiv;

last() --指定最后一个节点,

     driver.findElement(By.xpath("(//div[@class='bt_class'])[last()]"))

  

ps. 用xpath定位元素,若验证其正确与否,可在console使用方法$x(xpath).  e.g. :$x("//div[@id='ur']")

 

 

再简单说下css,个人总结其语法规则

空格后面是tag名

#后为id

. 后为class

例如 用css定位如下Yes 的button, 其值可写成

.class_6 button.btn-yes

[原创] Selenium By 查找页面元素

 

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-08-04
  • 2022-12-23
  • 2021-10-11
  • 2021-05-24
  • 2021-10-20
  • 2022-12-23
猜你喜欢
  • 2021-05-21
  • 2021-11-21
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-03
  • 2021-08-01
相关资源
相似解决方案