Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
694 views
in Technique[技术] by (71.8m points)

java - Selenium WebDriver Finding Element by Partial Class Name

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

And I have also tried with cssSelector:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<div class="dd algo algo-sr Sr" data-937="5d1abd07c5a33">
<div class="dd algo algo-sr fst Sr" data-0ab="5d1abd837d907">

Above 2 are the HTML elements in yahoo search results. So if we wanna get these elements using partial class name with selenium python, here is the solution.

driver.find_element_by_css_selector("div[class^='dd algo algo-sr']")

In the same way we can get any elements with partial match on any attribute values like class name, id etc.

find elements with css selector partial match on attribute values


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...