简体   繁体   中英

How to test if a WebElement Attribute exists using Selenium Webdriver

I am testing for whether elements on the page such as //img or //i have an alt attribute.

I can't find a way to detect when the attribute does not exist at all.

Here's the WebElement. Just an img with no alt attribute.

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

Here's my code trying to determine the alt's existence. I know it's not all necessary, I was just trying everything.

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

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

It seems like it is returning an empty string... For example, the following code returns "beforeAfter"

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

However, my if statement does not catch the return, so I don't know what to do.

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

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>

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

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'

Instead of checking the attribute, you should list the elements with the missing attribute with a selector:

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

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

I think you need to handle the null value first. if(null == we.getAttribute("alt")){ }

Here is the Answer to your Question:

Try to locate the WebElement through some other unique xpath as follows:

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

or

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

Let me know if this Answers your Question.

Assume we is the web element <img class="gsc-branding-img" ... .

  • we.getAttribute("blabla") returns null
  • we.getAttribute("class") returns "gsc-branding-img" So, we can check like this:
if(we.getAttribute("blabla") == null){
    System.out.println("Attribute does not exist");
}
else {
    System.out.println("Attribute exists!");
}

Similarly for python bindings of selenium:

we.get_attribute('blabla') returns None

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM