2017-06-27 5 views
0

以下の1 + 7の加算演算をテストしようとしました。しかし、わからない 属性 "name"が "Input"であるテキストフィールドの結果出力を取得する方法。Java selenium webdriver - テキストフィールドからテキストを取り出す

すべてのポインタが評価されます。


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> 
+0

By.id( "Input")を使って試してください。 –

+0

要素にはIDがありません。名前と型の属性のみを持っています。 – user1972031

+0

あなたは、最初の文章でidが "Input"である 'テキストフィールドと言っています。また、テストしているページのHTMLを投稿した場合にも役立ちます。 –

答えて

4

driver.findElement(By.name("Input")).getAttribute("value")を試してみてください。

element.getText()は、タグ内でテキストノードを取得するために使用されます(例: <tagname>text</tagname>)。

しかし、入力にはタグ内(ノードで表される)のテキストはありませんが、value属性内にはありません。たとえば、<input type="text" value="SomeValue">です。

ノードのテキストを要素内に入れたい場合はelement.getText()を使用する必要がありますが、入力の値を取得するにはelement.getAttribute("value")を使用する必要があります。

+0

値属性はありません。 – user1972031

+1

これがうまくいくと思います。https://stackoverflow.com/questions/7852287/using-selenium-web-driver-to-retrieve-value-of-a-html-input –

+0

こんにちは、@ user1972031! value属性がない場合は、入力の値またはテキストが空であることを意味します。この場合、element.getAttribute( "value")を呼び出すと、nullが返されます。 – Thisisalexis

関連する問題