如何使用 Selenium Webdriver 测试 WebElement 属性是否存在

dsi*_*ler 6 java selenium selenium-chromedriver selenium-webdriver

我测试的页面上的元素如是否//img//ialt属性。

我找不到一种方法来检测该属性何时根本不存在。

这是 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"/>
Run Code Online (Sandbox Code Playgroud)

这是我试图确定 alt 是否存在的代码。我知道这不是全部必要的,我只是在尝试一切。

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

if(altAttribute == null || altAttribute =="" || altAttribute == " ")
     {
     //attribute not found
     }
Run Code Online (Sandbox Code Playgroud)

好像是返回一个空字符串...例如下面的代码返回“beforeAfter”

System.out.println("before"+altAttribute+"After");
Run Code Online (Sandbox Code Playgroud)

但是,我的if语句没有赶上返回,所以我不知道该怎么办。

Gau*_*hah 5

if the attribute is not present, it should return null, if it's present and not set then it will return empty string. I think that's the case in your example, if so. Then you should use equal method to compare string rather than == operator.

Below example is about google search box, xxx attribute is not present for search box and so it will return 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");
    }
Run Code Online (Sandbox Code Playgroud)

You can try it out yourself by writing the following html and saving it as test. 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>
Run Code Online (Sandbox Code Playgroud)

Then write a web driver script that looks like this:

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") + "'");
Run Code Online (Sandbox Code Playgroud)

Which will give you the following output:

Does one have the data-se attribute?:  'null' 
Does two have the data-se attribute?:  '' 
Does three have the data-se attribute?:  'something'
Run Code Online (Sandbox Code Playgroud)


Flo*_* B. 3

您应该使用选择器列出缺少属性的元素,而不是检查属性:

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

if (elems.size() > 0) {
  // found images with alt attribute missing
}
Run Code Online (Sandbox Code Playgroud)