2017-06-30 22 views
2

//imgまたは//iなどの要素がalt属性を持つかどうかをテストしています。Selenium Webdriverを使用してWebElement属性が存在するかどうかをテストする方法

属性がまったく存在しないときを検出する方法が見つかりません。

ここにWebElementがあります。 alt属性のないimgだけです。

<img class="gsc-branding-img" src="https://www.google.com/cse/static/images/1x/googlelogo_grey_46x15dp.png" srcset="https://www.google.com/cse/static/images/2x/googlelogo_grey_46x15dp.png 2x"/> 

ここに私のコードは、altの存在を判断しようとしています。私はそれがすべて必要ではないことを知っている、私はすべてを試していた。

WebElement we == driver.findElement(By.xpath("(//img)[1]")); 
String altAttribute = we.getAttribute("alt"); 

if(altAttribute == null || altAttribute =="" || altAttribute == " ") 
    { 
    //attribute not found 
    } 

それは、空の文字列を返すように私ドンので、しかし、私のif文はリターンをキャッチしていない「beforeAfter」

System.out.println("before"+altAttribute+"After"); 

次のコードに戻り、例えば...らしいです何をすべきかわかりません。次のように他のいくつかのユニークなxpathを通じてWebElementを検索する

試してみてください:

WebElement we = driver.findElement(By.xpath("(//img[@class='gsc-branding-img']")); 

または

WebElement we = driver.findElement(By.xpath("//img[contains(@src,'googlelogo_grey_46x15dp.png')]")); 

私にしてみましょう。ここ

答えて

2

を、あなたがすべきです要素が欠落している要素をセレクタで一覧表示します。

List<WebElement> elems = driver.findElements(By.cssSelector("img:not([alt])")); 

if (elems.size() > 0) { 
    // found images with alt attribute missing 
} 
0

は、あなたの質問への答えがありますこれがあなたの質問に答えるかどうかを知る。

+0

ありがとうございました。それは私の例ではうまくいくでしょう。しかし、実際には、リストリスト allImages = driver.findElements(By.xpath( "// img"));のリストを反復処理しています。これらはすべてalt属性を持っているかどうかを確認する必要があります。それぞれ異なるxpathでそれぞれを分離する。私は私の例でもっと具体的にすべきだった。 – dsidler

0

まず、null値を処理する必要があると思います。属性が存在しない場合、それは存在だし、それが空の文字列を返します。設定されていない場合 if(null == we.getAttribute("alt")){ }

1

、それは、ヌルを返す必要があります。もしそうならあなたの例ではそうだと思います。次に==演算子ではなく文字列を比較するには、equalメソッドを使用する必要があります。例以下は

は、Google検索ボックスについてです、XXX属性は、検索ボックスのために存在していないので、それはあなたが次のHTMLを書いて保存することで、自分でそれを試してみることができますヌル

driver = new ChromeDriver(); 
    driver.get("http://google.com"); 
    WebElement ele = driver.findElement(By.name("q")); 
    String attr = ele.getAttribute("xxx"); 
    if (attr == null){ 
     System.out.print("Attribute does not exist"); 
    } 

    else if (attr.equals("")){ 
     System.out.print("Attribute is empty string"); 
    } 

を返します。それはテストとして。HTML:

driver.get("file:///<PATH_TO_HTML_ABOVE>/test.html"); 

WebElement one = driver.findElement(By.id("one")); 
WebElement two = driver.findElement(By.id("two")); 
WebElement three = driver.findElement(By.id("three")); 

System.out.println("Does one have the data-se attribute?: '" + one.getAttribute("data-se") + "'"); 
System.out.println("Does two have the data-se attribute?: '" + two.getAttribute("data-se") + "'"); 
System.out.println("Does three have the data-se attribute?: '" + three.getAttribute("data-se") + "'"); 

あなたの次の出力与える:

<html> 
    <head> 
    </head> 
    <body> 
     <p id="one">one</p> 
     <p id="two" data-se="">two</p> 
     <p id="three" data-se="something">three</p> 
    </body> 
</html> 

次に、このように見えるウェブドライバスクリプト書く代わりに属性をチェックする

Does one have the data-se attribute?: 'null' 
Does two have the data-se attribute?: '' 
Does three have the data-se attribute?: 'something' 
+0

DoubleとこれがChromeとFirefoxで100%正しいことを確認しました – Ardesco

関連する問題