2

Script:

List<WebElement> addCheck=driver.findElements(By.name("ticketLess"));
for(WebElement checkbox : addCheck ){
    System.out.println(checkbox.getAttribute("Text"));
}

HTML:

<input type="checkbox" name="ticketLess" value="checkbox">
<font face="Arial, Helvetica, sans-serif" size="2">
        Same as Billing Address&nbsp;</font>

The text i am trying to get is Same as Billing Address. I tried using getText() also but its not returning nothing.

Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
Combospirit
  • 313
  • 1
  • 2
  • 11

2 Answers2

2
List<WebElement> addCheck = driver.findElements(By.xpath(".//input[@name='ticketLess']/following-sibling::font"));
for(WebElement checkbox : addCheck ){
    System.out.println(checkbox.getText());
}

The selector used above selects next sibling of input, that is <font>. You need to get the font element to retrieve the text you want.

ThreeDots
  • 617
  • 7
  • 13
1

Actually you're getting wrong attribute name on wrong element to getting text. input element doesn't contain inner text.

You need to locate font element because text is present inside font element and use .getText() to getting this text as below :-

List<WebElement> addCheck = driver.findElements(By.cssSelector("input[name = 'ticketLess'] + font"));
for(WebElement checkbox : addCheck ){
    System.out.println(checkbox.getText());
}

Note :- If there are multiple checkboxes with the same locator and you want to get all text of these checkboxes font then you should use above code, otherwise if you can get text only this single checkbox font using findElement instead as below:-

WebElement checkbox = driver.findElement(By.cssSelector("input[name = 'ticketLess'] + font"));
System.out.println(checkbox.getText());
Saurabh Gaur
  • 23,507
  • 10
  • 54
  • 73
  • 1
    Thanks a bunch it worked.Is there any other method other than cssSelector and xpath which can perform this action? – Combospirit Aug 27 '16 at 22:09
  • Yes, you can get same thing using `xpath` locator as well, you can use xpath as `.//input[@name = 'ticketLess']/following::font`. Other than these locators there is no possible locator in this case to locate. – Saurabh Gaur Aug 27 '16 at 22:14