Selenium Webdriver 获取已经具有 value 属性的输入值 (Java) [英] Selenium Webdriver get input value which already has value attribute (Java)
问题描述
我需要解析一个包含输入元素的表格.使用 Selenium WebDriver 获取输入元素列表以提取其值:
I need to parse a table containing input elements in it. Using Selenium WebDriver I get the list of input elements to extract their values:
@FindBy(xpath = "xpathSelector")
private List<WebElement> table;
我有一个输入元素的例子:
An example of input element I have:
<input id="pt1:r1:3:detailTable:1:j_idt680::content"
name="pt1:r1:3:detailTable:1:j_idt680" style="text-align: right;color:#333333;"
class="af_inputText_content" type="text" value="1,000.00">
遍历输入网络元素,我需要获取每个值.
Iterating through input web elements I need to get each value.
List<String> inputValues = new ArrayList<>();
for (WebElement input : table) {
inputValues.add(getValue(input));
}
我可以使用
input.getAttribute("value");
但这不起作用,因为输入元素已经有一个定义的属性值".
But this doesn't work because input elements already have a defined attribute "value".
我发现在这种情况下我可以使用 JavaScript.这是我正在尝试的代码:
I found out that in this case I can use JavaScript. This is the code I'm trying:
public String getValue(input) {
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript("return arguments[0].value", element).toString();
}
但我得到的不是真正的值,而是 脏",这没有任何意义.
But instead of real values I get "dirty" which doesn't make any sense.
我能够使用以下方法在调试器模式下获得实际值:
I was able to get the real value in debugger mode using:
((JavascriptExecutor)driver).executeScript("return document.evaluate(\"xpathSelector\",
document, null, XPathResult.FIRST_ORDERED_NODE_TYPE,null).singleNodeValue.value")
.toString()
如何使用 Selenium Webdriver WebElements 已经找到的方法通过 JavascriptExecutor 获取值?
How can I get values through JavascriptExecutor using already found by Selenium Webdriver WebElements?
更新:我找到了使用 css 选择器解决此问题的方法,但我不喜欢它:
UPDATE: I found a workaround for the issue using css selector but I don't like it:
public String getNthInputValueByCss(String cssSelector, int elementNumber) {
String jsScript = String.format("return document.querySelectorAll(\"%s\")[%d].value", cssSelector, elementNumber);
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript(jsScript).toString();
}
推荐答案
我的进一步研究使我找到了使用 cssSelector 的解决方案.
My further research lead me to a solution with the use of cssSelector.
从文档到 Selenium WebDriver executeScript(String script, Object... args)
方法:
From the documentation to Selenium WebDriver executeScript(String script, Object... args)
method:
这些参数将通过参数"魔法变量,就好像函数是通过函数.应用"
The arguments will be made available to the JavaScript via the "arguments" magic variable, as if the function were called via "Function.apply"
参数必须是数字、布尔值、字符串、WebElement 或列表以上任意组合.
Arguments must be a number, a boolean, a String, WebElement, or a List of any combination of the above.
@param args 脚本的参数.可能为空
@param args The arguments to the script. May be empty
解决方案:
因此我们可以将我们的 WebElement 元素
作为 executeScript()
方法的参数之一传递,然后使用该参数在我们的 element<的子元素中进行搜索/代码>.
So we can pass our WebElement element
as one of the arguments of executeScript()
method and then use the argument to search among children of our element
.
protected String getInputValue(WebElement element) {
String js = "return arguments[0].querySelector('input').value";
JavascriptExecutor js = (JavascriptExecutor) driver;
return js.executeScript(js, element).toString();
}
这篇关于Selenium Webdriver 获取已经具有 value 属性的输入值 (Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!