简体   繁体   English

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

[英]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.我正在测试页面上的元素(例如//img//i是否具有alt属性。

I can't find a way to detect when the attribute does not exist at all.我找不到一种方法来检测该属性何时根本不存在。

Here's the WebElement.这是 WebElement。 Just an img with no alt attribute.只是一个没有 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"/>

Here's my code trying to determine the alt's existence.这是我试图确定 alt 是否存在的代码。 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"好像是返回一个空字符串……比如下面的代码返回“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语句没有捕获返回值,所以我不知道该怎么做。

if the attribute is not present, it should return null , if it's present and not set then it will return empty string.如果属性不存在,它应该返回null ,如果它存在并且没有设置,那么它将返回空字符串。 I think that's the case in your example, if so.如果是这样,我认为在您的示例中就是这种情况。 Then you should use equal method to compare string rather than == operator.那么你应该使用equal方法来比较字符串而不是==运算符。

Below example is about google search box, xxx attribute is not present for search box and so it will return null下面的例子是关于谷歌搜索框的,搜索框不存在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");
    }

You can try it out yourself by writing the following html and saving it as test.您可以通过编写以下 html 并将其保存为 test.html 来自己尝试一下。 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>

Then write a web driver script that looks like this:然后编写一个如下所示的 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") + "'");

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:尝试通过其他一些唯一的xpath定位WebElement ,如下所示:

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是 web 元素<img class="gsc-branding-img" ...

  • we.getAttribute("blabla") returns null we.getAttribute("blabla")返回null
  • we.getAttribute("class") returns "gsc-branding-img" So, we can check like this: we.getAttribute("class")返回"gsc-branding-img"所以,我们可以这样检查:
if(we.getAttribute("blabla") == null){
    System.out.println("Attribute does not exist");
}
else {
    System.out.println("Attribute exists!");
}

Similarly for python bindings of selenium:同样对于硒的python绑定:

we.get_attribute('blabla') returns None we.get_attribute('blabla')返回None

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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