Selenium WebDriver Finding Element by Partial Class Name
Asked Answered
M

4

8

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?

Multifoliate answered 20/11, 2014 at 21:2 Comment(0)
B
4

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

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

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

You can try something like this:

driver.findElement(By.xpath("//div[contains(text(),'"+expectedText+"')]")).click();
Brander answered 20/11, 2014 at 21:8 Comment(10)
You just forgot the // in front of the div but it worked once I made that correction! Thank you!Multifoliate
@Multifoliate Ah, man, you're right. Sorry about that. Fixed.Brander
No problem. It's not like you have the page source to work with so little typos like that are understandable when you can't test.Multifoliate
It's throwing an exception when expectedText includes single quotes. I tried escaping them with backslashes to no avail. Is there any way to be able to use single quotes in the expectedText?Multifoliate
@Multifoliate I found this: #12404370 - I hope it's useful.Brander
I tried replacing an apostrophe with &apos; but that didn't work either. Do you think it's just not possible?Multifoliate
@Multifoliate Give this a try: "//div[contains(text(),"+expectedText+")]" - I think removing the single quotes from around the expectedText might work. - See #13482852Brander
Yeah I had actually tried that before posting that first comment to you, and unfortunately that didn't work either. It's so strange that the xpath is so sensitive to the use of single quotes. Any other ideas? I may just post this as a separate question so I can get more eyes on it.Multifoliate
@Multifoliate I had a thought, but don't know how practical it is. Say you have a string "foo'bar". You could split the string on the single quote so you have expectedText="foo" and expectedText2="bar". Then if expectedText2 isn't null or equal to "", you could build a different findElement that had a selector like this: //div[contains(.,"expectedText") and contains(.,"expectedText2")] - I don't feel it's a terribly great solution, but it's the best I can come up with at this point.Brander
Wait did post an answer to this exact question a month ago in a post at this link: stackoverflow.com/questions/26242120/… ? Why did they down-vote you for this? It's a great question. But yeah, I was thinking along the same lines with the splitting and and'ing. It's not the most elegant but it may be the only way unfortunately ...Multifoliate
S
23
<div class="dd algo algo-sr Sr" data-937="5d1abd07c5a33">
<div class="dd algo algo-sr fst Sr" data-0ab="5d1abd837d907">

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

# for selenium old version:
driver.find_element_by_css_selector("div[class^='dd algo algo-sr']")

# for selenium new version
driver.find_element(By.CSS_SELECTOR, "div[class^='dd algo algo-sr']")

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

find elements with css selector partial match on attribute values: find elements with css selector partial match on attribute values

Speroni answered 2/7, 2019 at 2:43 Comment(0)
B
4

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

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

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

You can try something like this:

driver.findElement(By.xpath("//div[contains(text(),'"+expectedText+"')]")).click();
Brander answered 20/11, 2014 at 21:8 Comment(10)
You just forgot the // in front of the div but it worked once I made that correction! Thank you!Multifoliate
@Multifoliate Ah, man, you're right. Sorry about that. Fixed.Brander
No problem. It's not like you have the page source to work with so little typos like that are understandable when you can't test.Multifoliate
It's throwing an exception when expectedText includes single quotes. I tried escaping them with backslashes to no avail. Is there any way to be able to use single quotes in the expectedText?Multifoliate
@Multifoliate I found this: #12404370 - I hope it's useful.Brander
I tried replacing an apostrophe with &apos; but that didn't work either. Do you think it's just not possible?Multifoliate
@Multifoliate Give this a try: "//div[contains(text(),"+expectedText+")]" - I think removing the single quotes from around the expectedText might work. - See #13482852Brander
Yeah I had actually tried that before posting that first comment to you, and unfortunately that didn't work either. It's so strange that the xpath is so sensitive to the use of single quotes. Any other ideas? I may just post this as a separate question so I can get more eyes on it.Multifoliate
@Multifoliate I had a thought, but don't know how practical it is. Say you have a string "foo'bar". You could split the string on the single quote so you have expectedText="foo" and expectedText2="bar". Then if expectedText2 isn't null or equal to "", you could build a different findElement that had a selector like this: //div[contains(.,"expectedText") and contains(.,"expectedText2")] - I don't feel it's a terribly great solution, but it's the best I can come up with at this point.Brander
Wait did post an answer to this exact question a month ago in a post at this link: stackoverflow.com/questions/26242120/… ? Why did they down-vote you for this? It's a great question. But yeah, I was thinking along the same lines with the splitting and and'ing. It's not the most elegant but it may be the only way unfortunately ...Multifoliate
L
0
driver.findElement(By.xpath("//div[contains(@class,'" + partialClassNameMatcherText + "')]")).click();

^ this is useful especially in context of composed & dynamic classes which switch based on element interaction

Latter answered 28/8, 2024 at 7:46 Comment(0)
S
-8

I believe this will work:

driver.findElement(By.className("x-grid3-cell-inner x-grid3-col-expRepCol").click();
Salaam answered 20/11, 2014 at 21:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.