1

このコードを実行すると、空の文字列値が返されます。誰でもこの問題を解決するように提案できますか? ここでは gettext() メソッドを使用しました。リンク名は取得しません。

私のコードは次のとおりです。

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");
        }
    }  
}

}

出力は次のとおりです。

Size is: 3    
is selected    
is not selected 
is selected
PASSED: CheckingChkbox
4

4 に答える 4

6

アプリケーションに関しては、getText が内部テキストを返すgetAttribute("value")代わりに使用する必要がある場合があります。getText()

于 2013-04-10T10:15:33.900 に答える
1

ページの HTML を確認すると、タグに内部テキストはありません。したがって、使用できませんgetText()

入力タグの値を取得しようとしていると思います。HTMl agian を確認すると、input タグに value 属性があります。次を使用してその値を読み取ることができます。 getAttribute("value")

于 2013-04-10T10:39:12.507 に答える
0

「。」を削除してみてください。xpathの前に、xpath要素が正しいことを確認してください

これを試してdriver.findElement(By.id("fruits")).getText());

于 2013-04-10T11:56:01.047 に答える
0

Java とそのツールの能力を利用して、「より良い」プログラミング方法に変更しました。

実際には getText() を使用して、次のようなタグ間のテキストをキャッチします

<input id="input1" value="123"> getText() catches here </input> 

getAttribute() は、指定された属性の値をキャッチします。

<input id="input1" value=" getAttribute("value") catches here ">123</input>

それが以下のコードの私のバージョンです。

@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()
于 2013-04-11T06:54:31.393 に答える