[英]Java selenium webdriver - get text out of a text field
我試圖在下面測試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();
}
}
下面列出了相關代碼的HTML:
<td>
<div align="center"> <font size="+1">
<input name="Input" type="text" size="16">
</font></div>
</td>
嘗試driver.findElement(By.name("Input")).getAttribute("value")
。
element.getText()
用於獲取標簽內的文本節點,例如<tagname>text</tagname>
。
但是,輸入的標簽中沒有文本(由節點表示),而是在value屬性中。 例如: <input type="text" value="SomeValue">
。
因此,如果要獲取元素內的節點文本,則必須使用element.getText()
,但是如果要獲取輸入的值,則必須使用element.getAttribute("value")
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.