简体   繁体   中英

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: 在此处输入图片说明

HTML screenshot:

在此处输入图片说明

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

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]

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.

您可以尝试: .getAttribute("value");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM