Reputation: 11
I wrote the following, after run this code it returns empty String value. Can any one suggest me to solve this problem? Here I used gettext() method. It does not retrieve the link names.
My code is:
package Practice_pack_1;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class CheckingUncheckingCheckbox {
WebDriver driver;
@BeforeTest
public void open()
{
driver=new FirefoxDriver();
driver.navigate().to("http://openwritings.net/sites/default/files/radio_checkbox.html");
}
@AfterTest
public void teardown() throws InterruptedException
{
Thread.sleep(3000);
driver.quit();
}
@Test
public void CheckingChkbox() throws InterruptedException{
WebElement parent = driver.findElement(By.xpath(".//*[@id='fruits']"));
List<WebElement> children = parent.findElements(By.tagName("input"));
int sz= children.size();
System.out.println("Size is: "+sz);
for (int i = 0; i <sz; i++)
{
boolean check= children.get(i).isSelected();
if(check==true)
{
System.out.println(children.get(i).getText()+ "is selected");
}
else
{
System.out.println(children.get(i).getText()+ "is not selected");
}
}
}
}
Output is:
Size is: 3
is selected
is not selected
is selected
PASSED: CheckingChkbox
Upvotes: 1
Views: 9338
Reputation: 1251
I've changed to way you programming to a "better" one, using the capacity of java and its tools.
Actually the getText() is used to catch the text bewteen the tag like
<input id="input1" value="123"> getText() catches here </input>
and getAttribute() catches the value of a specified attribute.
<input id="input1" value=" getAttribute("value") catches here ">123</input>
That's my version of your code below.
@Test
public void CheckingChkbox() throws InterruptedException{
WebElement parent = driver.findElement(By.xpath(".//*[@id='fruits']"));
List<WebElement> children = parent.findElements(By.tagName("input"));
System.out.println("Size is: "+children.size());
for (WebElement el : children)
{
if(el.isSelected())
{
System.out.println(el.getAttribute("value")+ "is selected");
}
else
{
System.out.println(el.getAttribute("value")+ "is not selected");
}
} // end for
}// end CheckingChkbox()
Upvotes: 0
Reputation: 183
Try to remove the "." before your xpath, and make sure your xpath element is correct
try this driver.findElement(By.id("fruits")).getText());
Upvotes: 0
Reputation: 339
if you go and check your page HTML there is no inner text lies in tag. So you can't use getText()
.
I assume you are looking to get value of the input tags. If you check your HTMl agian there is a value attribute in input tag. You can read that value using,
getAttribute("value")
Upvotes: 1
Reputation: 787
Regarding your application you may need to use getAttribute("value")
instead of getText()
as getText return the inner text.
Upvotes: 6