TodayILearned
TodayILearned

Reputation: 1500

How to grab text corresponding to checkboxes in selenium webdriver?

I am using selenium webdriver to run script.

I have a scenario where I want to grab text corresponding to checkboxes.

For a single checkbox I am using getAttribute() to capture text as given in below code and it is working fine.

String referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']//input")).getAttribute("id"); 

Does getAttribute invalid in case of findElements() ?

How to capture text for multiple checkbox?

Screenshot of checkbox: enter image description here

HTML screenshot:

enter image description here

As you can see in screenshot I want the attribute - id to capture my text.

Upvotes: 1

Views: 2021

Answers (3)

Manu
Manu

Reputation: 2291

Just taking the code from you another question and modifying it.

String referenceIn3DPage =null;
int count=Driver.driver.findElements(By.xpath("//div[3][@class='viewer3d-demo-commercial-references-checkboxes']//input")).size();
System.out.println("the count="+count);

for(int i=1;i<=count;i++)
{
  referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='viewer3d-demo-commercial-references-checkboxes']/div["+i+"]/label/input")).getAttribute("id"); 
  System.out.println("the value in 3d= "+referenceIn3DPage);
}

Kindly run the above code and let me know if it gives you the expected result.

Upvotes: 1

Helping Hands
Helping Hands

Reputation: 5396

Kindly use value parameter to get text of checkbox :

String referenceIn3DPage=Driver.driver.findElement(By.xpath("//div[3][@class='some-class']//input")).getAttribute("value");

Output : OSCP120 [If given xpath is of first check box]

Upvotes: 1

Prasad
Prasad

Reputation: 25

You can try : .getAttribute("value");

Upvotes: 0

Related Questions