get a class name

25 views
Skip to first unread message

Eko Budiharto

unread,
Jan 10, 2018, 10:45:28 PM1/10/18
to Selenium Users
dear all,
I have a question. I am trying to get the ID value using this xpath from below span line -> String className=driver.findElement(By.xpath("//span[contains(text(),'Account')]")).

<span id="page:frm:pbAddInterfaces:pbTbl:0:j_id63">AccountA</span>

I am using the getText() -> String className=driver.findElement(By.xpath("//span[contains(text(),'Account')]")).getAttribute("id").toString();, I still cannot get the id value.

please help.



brocco...@gmail.com

unread,
Jan 17, 2018, 9:00:21 PM1/17/18
to Selenium Users
Hello,
your code looks correct.
I have created this page
<html>
<body>

<span id="page:frm:pbAddInterfaces:pbTbl:0:j_id63">AccountA</span>
</body>
</html>

and executed this:

String className=driver.findElement(By.xpath("//span[contains(text(),'Account')]")).getAttribute("id").toString();

returns id:
className returned "page:frm:pbAddInterfaces:pbTbl:0:j_id63"

if you are getting different results, you may have multiple matches on the page or you may need to switch to an iframe.
try identifying matching objects
List<WebElement> objMatches = driver.findElements(By.xpath("//span[contains(text(),'Account')]"));
or see if object exists in an iframe and if so, switch to iframe first.

Eko Budiharto

unread,
Jan 19, 2018, 12:17:08 AM1/19/18
to Selenium Users
thank you, broccolislayr. It solved.
Reply all
Reply to author
Forward
0 new messages