6

cssGetValue メソッドを使用して、特定の Web 要素の CSS プロパティから値を取得する演習を行っています。

2 つの質問があります。

  1. cssGetValue メソッドが値 13px を返した理由、実際に参照されたメソッドを実行する Web 要素。1a. 「ID別」というラベルの付いたセクションのCSSプロパティを取得したい。id="by-id" セクションの CSS プロパティ値を取得するには、コードをどのように変更すればよいですか?

  2. driver.close() メソッドを使用しましたが、スクリプトの終了後にブラウザーが閉じません。この場合、driver.close() メソッドが機能しなかった理由を説明してください。

    ここに私のコードフラグメントがあります:

    package wd_findElementBy;
    
    import java.util.List;
    
    import org.junit.Test;
    
    import org.junit.Before;
    
    import org.junit.After;
    
    
    import org.openqa.selenium.By;
    
    import org.openqa.selenium.WebDriver;
    
    import org.openqa.selenium.WebElement;
    
    import org.openqa.selenium.firefox.FirefoxDriver;
    
    
    public class SearchWebElements 
    {
    
    WebDriver driver = new FirefoxDriver();
    private String baseUrl= "http://docs.seleniumhq.org/docs/03_webdriver.jsp#introducing-the-selenium-webdriver-api-by-example";
    
    @Test
    public void findElements(){
    driver.get(baseUrl);
    
    try{
        List<WebElement> elements = driver.findElements(By.id("by-id"));
        System.out.println("number of elements: " + elements.size());
    
        for(WebElement ele : elements){
            System.out.println(ele.getTagName());
    
            System.out.println("get the text for web element with id='by-id' ");
            System.out.println("------------------------------------------------------------");
            System.out.println(ele.getText());
            System.out.println("------------------------------------------------------------");
            System.out.println(ele.getAttribute("id"));
            System.out.println(ele.getCssValue("font-size"));
    
        }
    }
    
    finally{
        //driver.close();
        driver.quit();
    }
    
    
    }
    
    }
    
4

4 に答える 4

13

はい、すべて正しいです。

font-sizeFirebugで検索できる場所のスクリーンショットを次に示します。

ここに画像の説明を入力

ID は (少なくともこのページでは) 一意であるはずなので、ID をfindElements持つ要素のリストを見つけてby-idループする必要はありません。代わりにfindElement、要素を直接取得するために使用します。

try{
        WebElement byId = driver.findElement(By.id("by-id"));

        System.out.println(byId.getTagName());

        System.out.println("get the text for web element with id='by-id' ");
        System.out.println("------------------------------------------------------------");
        System.out.println(byId.getText());
        System.out.println("------------------------------------------------------------");
        System.out.println(byId.getAttribute("id"));
        System.out.println(byId.getCssValue("font-size"));
    }
}
于 2013-06-23T21:15:40.510 に答える
11

CSS 値を取得する場合:

driver.findElement(By.id("by-id")).getCssValue("font-size");//similarly you can use other CSS property such as background-color, font-family etc.

スクリプトの実行終了後にブラウザを終了/閉じるには:

driver.quit();
于 2014-01-23T11:21:52.677 に答える
1

パブリック クラス GetCssValues {

public WebDriver driver;
private By bySearchButton = By.name("btnK");

@BeforeClass
public void setUp() {
    driver = new FirefoxDriver();
    driver.get("http://www.google.com");
}

@Test(priority=1)
public void getCssValue_ButtonColor()  {
    WebElement googleSearchBtn = driver.findElement(bySearchButton); 
    System.out.println("Color of a button before mouse hover: " + googleSearchBtn.getCssValue("color"));
    Actions action = new Actions(driver);
    action.moveToElement(googleSearchBtn).perform();
    System.out.println("Color of a button after mouse hover : " + googleSearchBtn.getCssValue("color"));
}

@Test(priority=2)
public void getCssValue_ButtonFontSize() {
    WebElement googleSearchBtn = driver.findElement(bySearchButton);
    System.out.println("Font Size of a button " + googleSearchBtn.getCssValue("font-size"));
}

@Test(priority=3)
public void getCssValue_ButtonFontWeight(){
    WebElement googleSearchBtn = driver.findElement(bySearchButton);
    System.out.println("Font Weight of a button "   +getFontWeight(googleSearchBtn) );
}

public String getFontWeight(WebElement element) {
    //Output will return as 400 for font-weight : normal, and 700 for font-weight : bold
    return element.getCssValue("font-weight");
}

@AfterClass
public void tearDown() {
    driver.quit();
}

}

出力:

マウスホバー前のボタンの色: rgba(68, 68, 68, 1) マウスホバー後のボタンの色: rgba(34, 34, 34, 1) ボタンのフォントサイズ 11px ボタンのフォントウェイト 700

于 2015-06-10T09:56:54.050 に答える