如何使用XPath获取属性的值

时间:2022-11-26 23:06:41

I have been testing using Selenium WebDriver and I have been looking for an XPath code to get the value of the attribute of an HTML element as part of my regression testing. But I couldn't find a good answer.

我一直在使用Selenium WebDriver进行测试,我一直在寻找一个XPath代码来获取HTML元素属性的值作为回归测试的一部分。但我找不到一个好的答案。

Here is my sample html element:

这是我的示例html元素:

<div class="firstdiv" alt="testdiv"></div>

I want to get the value of the "alt" attribute using the XPath. I have an XPath to get to the div element using the class attribute which is:

我想使用XPath获取“alt”属性的值。我有一个XPath来使用class属性获取div元素,该属性是:

//div[@class="firstdiv"]

Now, I am looking for an XPath code to get the value of the "alt" attribute. The assumption is that I don't know what is the value of the "alt" attribute.

现在,我正在寻找一个XPath代码来获取“alt”属性的值。假设是我不知道“alt”属性的值是多少。

4 个解决方案

#1


10  

You can use the getAttribute() method.

您可以使用getAttribute()方法。

driver.findElement(By.xpath("//div[@class='firstdiv']")).getAttribute("alt");

#2


0  

Using C#, .Net 4.5, and Selenium 2.45

使用C#,。Net 4.5和Selenium 2.45

Use findElements to capture firstdiv elements into a collection.

使用findElements将firstdiv元素捕获到集合中。

var firstDivCollection = driver.findElements(By.XPath("//div[@class='firstdiv']"));

Then iterate over the collection.

然后迭代集合。

        foreach (var div in firstDivCollection) {
            div.GetAttribute("alt");
        }

#3


0  

Just use executeScript and do XPath or querySelector/getAttribute in browser. Other solutions are wrong, because it takes forever to call getAttribute for each element from Selenium if you have more than a few.

只需使用executeScript并在浏览器中执行XPath或querySelector / getAttribute。其他解决方案是错误的,因为如果你有多个解决方案,那么从Selenium为每个元素调用getAttribute会花费很长时间。

  var hrefsPromise = driver.executeScript(`
        var elements = document.querySelectorAll('div.firstdiv');
        elements = Array.prototype.slice.call(elements);
        return elements.map(function (element) {
              return element.getAttribute('alt');
        });
  `);

#4


0  

Selenium Xpath can only return elements. You should pass javascript function that executes xpaths and returns strings to selenium.

Selenium Xpath只能返回元素。你应该传递执行xpaths的javascript函数并将字符串返回给selenium。

I'm not sure why they made it this way. Xpath should support returning strings.

我不确定他们为什么这么做。 Xpath应该支持返回字符串。

#1


10  

You can use the getAttribute() method.

您可以使用getAttribute()方法。

driver.findElement(By.xpath("//div[@class='firstdiv']")).getAttribute("alt");

#2


0  

Using C#, .Net 4.5, and Selenium 2.45

使用C#,。Net 4.5和Selenium 2.45

Use findElements to capture firstdiv elements into a collection.

使用findElements将firstdiv元素捕获到集合中。

var firstDivCollection = driver.findElements(By.XPath("//div[@class='firstdiv']"));

Then iterate over the collection.

然后迭代集合。

        foreach (var div in firstDivCollection) {
            div.GetAttribute("alt");
        }

#3


0  

Just use executeScript and do XPath or querySelector/getAttribute in browser. Other solutions are wrong, because it takes forever to call getAttribute for each element from Selenium if you have more than a few.

只需使用executeScript并在浏览器中执行XPath或querySelector / getAttribute。其他解决方案是错误的,因为如果你有多个解决方案,那么从Selenium为每个元素调用getAttribute会花费很长时间。

  var hrefsPromise = driver.executeScript(`
        var elements = document.querySelectorAll('div.firstdiv');
        elements = Array.prototype.slice.call(elements);
        return elements.map(function (element) {
              return element.getAttribute('alt');
        });
  `);

#4


0  

Selenium Xpath can only return elements. You should pass javascript function that executes xpaths and returns strings to selenium.

Selenium Xpath只能返回元素。你应该传递执行xpaths的javascript函数并将字符串返回给selenium。

I'm not sure why they made it this way. Xpath should support returning strings.

我不确定他们为什么这么做。 Xpath应该支持返回字符串。