簡體   English   中英

如何使用 Selenium Webdriver 測試 WebElement 屬性是否存在

[英]How to test if a WebElement Attribute exists using Selenium Webdriver

我正在測試頁面上的元素(例如//img//i是否具有alt屬性。

我找不到一種方法來檢測該屬性何時根本不存在。

這是 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
     }

好像是返回一個空字符串……比如下面的代碼返回“beforeAfter”

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

但是,我的if語句沒有捕獲返回值,所以我不知道該怎么做。

如果屬性不存在,它應該返回null ,如果它存在並且沒有設置,那么它將返回空字符串。 如果是這樣,我認為在您的示例中就是這種情況。 那么你應該使用equal方法來比較字符串而不是==運算符。

下面的例子是關於谷歌搜索框的,搜索框不存在xxx屬性,因此它將返回null

    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 並將其保存為 test.html 來自己嘗試一下。 html:

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

然后編寫一個如下所示的 Web 驅動程序腳本:

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") + "'");

這將為您提供以下輸出:

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

您應該使用選擇器列出缺少屬性的元素,而不是檢查屬性:

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

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

我認為您需要先處理空值。 if(null == we.getAttribute("alt")){ }

以下是您問題的答案:

嘗試通過其他一些唯一的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')]"));

如果這能回答您的問題,請告訴我。

假設we是 web 元素<img class="gsc-branding-img" ...

  • we.getAttribute("blabla")返回null
  • we.getAttribute("class")返回"gsc-branding-img"所以,我們可以這樣檢查:
if(we.getAttribute("blabla") == null){
    System.out.println("Attribute does not exist");
}
else {
    System.out.println("Attribute exists!");
}

同樣對於硒的python綁定:

we.get_attribute('blabla')返回None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM