Selenium WebDriver查找元素(按部分类名称) [英] Selenium WebDriver Finding Element by Partial Class Name

查看:344
本文介绍了Selenium WebDriver查找元素(按部分类名称)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我正在使用的框架中,我具有以下元素:

In the frame I'm working with, I have the following element:

<div class="x-grid3-cell-inner x-grid3-col-expRepCol">  New session from client IP 192.168.5.3 (ST=/CC=/C=) at VIP 192.168.5.2 Listener /Common/Tomcat (Reputation=Unknown)</div>

以及许多其他类似元素.我正在尝试通过部分名称文本查找此元素,然后使用以下代码单击它:

As well as many other similar elements. I am trying to locate this element by partial name text and click it with the following code:

String expectedText = "New session from client IP";
driver.findElement(By.className("div[class*='"+expectedText+"']")).click();

我也尝试过使用cssSelector:

And I have also tried with cssSelector:

String expectedText = "New session from client IP";
driver.findElement(By.cssSelector("div[class*='"+expectedText+"']")).click();

但是WebDriver不断抛出异常,表明无法找到该元素.关于可能是什么问题的任何建议?

But WebDriver keeps throwing an exception stating it's unable to locate that element. Any suggestions as to what could be the problem?

推荐答案

By.className正在寻找一个具有输入名称的类.

By.className is looking for a class with the name entered.

By.cssSelector正在寻找与您输入的选择器匹配的内容.

By.cssSelector is looking for a match for the selector you entered.

您要尝试的是将div的文本与class匹配,这是行不通的.

What you're attempting is to match the text of the div against class, which won't work.

您可以尝试以下操作:

driver.findElement(By.xpath("//div[contains(text(),'"+expectedText+"')]")).click();

这篇关于Selenium WebDriver查找元素(按部分类名称)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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