use*_*031 1 java selenium-webdriver
我试图在下面测试1 + 7的加法运算;但是不知道如何获取属性“名称”为“输入”的文本字段的结果输出。
任何指针将不胜感激。
package hw9;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class calculator {
private WebDriver driver;
private String baseUrl;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.math.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void testCalculator() throws Exception {
driver.get(baseUrl + "/students/calculators/source/basic.htm");
driver.findElement(By.name("one")).click();
driver.findElement(By.name("plus")).click();
driver.findElement(By.name("seven")).click();
driver.findElement(By.name("DoIt")).click();
String output = driver.findElement(By.name("Input")).getText();
System.out.println("Output: " + output); // **<--- Empty output**
assertEquals(8,output);
}
@After
public void tearDown() throws Exception {
driver.quit();
}
}
Run Code Online (Sandbox Code Playgroud)
下面列出了相关代码的HTML:
<td>
<div align="center"> <font size="+1">
<input name="Input" type="text" size="16">
</font></div>
</td>
Run Code Online (Sandbox Code Playgroud)
尝试driver.findElement(By.name("Input")).getAttribute("value")
。
element.getText()
用于获取标签内的文本节点,例如
<tagname>text</tagname>
。
但是,输入的标签中没有文本(由节点表示),而是在value属性中。例如:<input type="text" value="SomeValue">
。
因此,如果要获取元素内的节点文本,则必须使用element.getText()
,但是如果要获取输入的值,则必须使用element.getAttribute("value")
。
归档时间: |
|
查看次数: |
2422 次 |
最近记录: |