Java 为什么GetText方法返回空字符串
我编写了以下代码,在运行此代码后,它返回空字符串值。有人能建议我解决这个问题吗? 这里我使用了gettext()方法。它不会检索链接名称 我的代码是:Java 为什么GetText方法返回空字符串,java,selenium-webdriver,Java,Selenium Webdriver,我编写了以下代码,在运行此代码后,它返回空字符串值。有人能建议我解决这个问题吗? 这里我使用了gettext()方法。它不会检索链接名称 我的代码是: package Practice_pack_1; import java.util.List; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement;
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");
}
}
}
关于应用程序,可能需要使用
getAttribute(“value”)
而不是getText()
,因为getText返回内部文本。关于应用程序,可能需要使用getAttribute(“value”)
而不是getText()
as getText返回内部文本。如果您检查页面HTML,则标记中没有内部文本。因此,您不能使用getText()
我假设您希望获得输入标记的值。如果您再次检查HTMl,则输入标记中有一个value属性。您可以使用,
getAttribute(“value”)
如果您去检查页面HTML,则标记中没有内部文本。因此,您不能使用getText()
我假设您希望获得输入标记的值。如果您再次检查HTMl,则输入标记中有一个value属性。您可以使用,
getAttribute(“value”)
尝试删除xpath之前的“.”,并确保xpath元素正确无误
试试这个driver.findElement(By.id(“fruits”).getText()代码>尝试删除xpath之前的“.”,并确保xpath元素正确无误
试试这个driver.findElement(By.id(“fruits”).getText()代码>我已经改变了使用java的能力编写“更好”代码的方式
以及它的工具
实际上,getText()用于捕获标记中的文本,如
<input id="input1" value="123"> getText() catches here </input>
getText()在这里捕获
getAttribute()捕获指定属性的值
<input id="input1" value=" getAttribute("value") catches here ">123</input>
123
下面是我的代码版本
@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()
@测试
public void CheckingChkbox()引发中断异常{
WebElement parent=driver.findElement(By.xpath(“./*[@id='fruits']”);
列表子项=父.findElements(按.tagName(“输入”));
System.out.println(“大小为:+children.Size());
for(WebElement el:children)
{
if(el.isSelected())
{
System.out.println(选择el.getAttribute(“值”)+);
}
其他的
{
System.out.println(el.getAttribute(“值”)+“未选中”);
}
}//结束
}//结束检查chkbox()
我已经改变了您的编程方式,使用java的能力,使之成为一种“更好”的编程方式
以及它的工具
实际上,getText()用于捕获标记中的文本,如
<input id="input1" value="123"> getText() catches here </input>
getText()在这里捕获
getAttribute()捕获指定属性的值
<input id="input1" value=" getAttribute("value") catches here ">123</input>
123
下面是我的代码版本
@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()
@测试
public void CheckingChkbox()引发中断异常{
WebElement parent=driver.findElement(By.xpath(“./*[@id='fruits']”);
列表子项=父.findElements(按.tagName(“输入”));
System.out.println(“大小为:+children.Size());
for(WebElement el:children)
{
if(el.isSelected())
{
System.out.println(选择el.getAttribute(“值”)+);
}
其他的
{
System.out.println(el.getAttribute(“值”)+“未选中”);
}
}//结束
}//结束检查chkbox()