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();