In the HTML of a webapp there is the following code
在webapp的HTML中,有以下代码
<input type="text" name="prettyTime" id="prettyTime" class="ui-state-disabled prettyTime" readonly="readonly">
What is actually shown on the page is a string displaying the time.
页面上实际显示的是显示时间的字符串。
In Selenium Web Driver, I have a WebElement
object referring to the <input>
using
在Selenium Web Driver中,我有一个引用的WebElement对象
WebElement timeStamp = waitForElement(By.id("prettyTime"));
I want to get the value of the WebElement
, or, in other words, what is printed on the page. I tried all the WebElement
getters and nothing has been retrieving the actual value that the user sees. Any help? Thanks.
我想获取WebElement的值,或者换句话说,获取页面上打印的内容。我尝试了所有WebElement getter,并没有检索到用户看到的实际值。有帮助吗?谢谢。
8 个解决方案
#1
146
Try element.getAttribute("value")
尝试element.getAttribute(“value”)
The text
property is for text within the tags of an element. For input elements, the displayed text is not wrapped by the <input>
tag, instead it's inside the value
attribute.
text属性用于元素标记内的文本。对于输入元素,显示的文本不会被标记包装,而是在value属性中。
Note: Case matters. If you specify "Value", you'll get a 'null' value back. This is true for C# at least.
注意:案件很重要。如果指定“Value”,则会返回“null”值。这至少对C#来说是这样的。
#2
17
You can do like this :
你可以这样做:
webelement time=driver.findElement(By.id("input_name")).getAttribute("value");
this will give you the time displaying on the webpage.
这将为您提供在网页上显示的时间。
#3
11
With selenium 2,
用硒2,
i usually write it like that :
我通常这样写:
WebElement element = driver.findElement(By.id("input_name"));
String elementval = element.getAttribute("value");
OR
要么
String elementval = driver.findElement(By.id("input_name")).getAttribute("value");
#4
5
Following @ragzzy 's answer I use
按照@ragzzy的回答我用
public static string Value(this IWebElement element, IJavaScriptExecutor javaScriptExecutor)
{
try
{
string value = javaScriptExecutor.ExecuteScript("return arguments[0].value", element) as string;
return value;
}
catch (Exception)
{
return null;
}
}
It works quite well and does not alter the DOM
它工作得很好,不会改变DOM
#5
4
As was mentioned before, you could do something like that
如前所述,你可以做那样的事情
public String getVal(WebElement webElement) {
JavascriptExecutor e = (JavascriptExecutor) driver;
return (String) e.executeScript(String.format("return $('#%s').val();", webElement.getAttribute("id")));
}
But as you can see, your element must have an id
attribute, and also, jquery on your page.
但正如您所看到的,您的元素必须具有id属性,并且页面上还有jquery。
#6
1
If the input value gets populated by a script that has some latency involved (e.g. AJAX call) then you need to wait until the input has been populated. E.g.
如果输入值由一个涉及一些延迟的脚本填充(例如AJAX调用),那么您需要等到输入已填充。例如。
var w = new WebDriverWait(WebBrowser, TimeSpan.FromSeconds(10));
w.Until((d) => {
// Wait until the input has a value...
var elements = d.FindElements(By.Name(name));
var ele = elements.SingleOrDefault();
if (ele != null)
{
// Found a single element
if (ele.GetAttribute("value") != "")
{
// We have a value now
return true;
}
}
return false;
});
var e = WebBrowser.Current.FindElement(By.Name(name));
if (e.GetAttribute("value") != value)
{
Assert.Fail("Result contains a field named '{0}', but its value is '{1}', not '{2}' as expected", name, e.GetAttribute("value"), value);
}
#7
0
This is kind of hacky, but it works.
这有点像hacky,但它确实有效。
I used JavascriptExecutor
and added a div
to the HTML, and changed the text in the div
to $('#prettyTime').val()
I then used Selenium to retrieve the div
and grab its value. After testing the correctness of the value, I removed the div that was just created.
我使用了JavascriptExecutor并在HTML中添加了div,并将div中的文本更改为$('#prettyTime')。val()然后我使用Selenium检索div并获取其值。在测试了值的正确性后,我删除了刚刚创建的div。
#8
0
For python bindings it will be :
对于python绑定,它将是:
element.get_attribute('value')
#1
146
Try element.getAttribute("value")
尝试element.getAttribute(“value”)
The text
property is for text within the tags of an element. For input elements, the displayed text is not wrapped by the <input>
tag, instead it's inside the value
attribute.
text属性用于元素标记内的文本。对于输入元素,显示的文本不会被标记包装,而是在value属性中。
Note: Case matters. If you specify "Value", you'll get a 'null' value back. This is true for C# at least.
注意:案件很重要。如果指定“Value”,则会返回“null”值。这至少对C#来说是这样的。
#2
17
You can do like this :
你可以这样做:
webelement time=driver.findElement(By.id("input_name")).getAttribute("value");
this will give you the time displaying on the webpage.
这将为您提供在网页上显示的时间。
#3
11
With selenium 2,
用硒2,
i usually write it like that :
我通常这样写:
WebElement element = driver.findElement(By.id("input_name"));
String elementval = element.getAttribute("value");
OR
要么
String elementval = driver.findElement(By.id("input_name")).getAttribute("value");
#4
5
Following @ragzzy 's answer I use
按照@ragzzy的回答我用
public static string Value(this IWebElement element, IJavaScriptExecutor javaScriptExecutor)
{
try
{
string value = javaScriptExecutor.ExecuteScript("return arguments[0].value", element) as string;
return value;
}
catch (Exception)
{
return null;
}
}
It works quite well and does not alter the DOM
它工作得很好,不会改变DOM
#5
4
As was mentioned before, you could do something like that
如前所述,你可以做那样的事情
public String getVal(WebElement webElement) {
JavascriptExecutor e = (JavascriptExecutor) driver;
return (String) e.executeScript(String.format("return $('#%s').val();", webElement.getAttribute("id")));
}
But as you can see, your element must have an id
attribute, and also, jquery on your page.
但正如您所看到的,您的元素必须具有id属性,并且页面上还有jquery。
#6
1
If the input value gets populated by a script that has some latency involved (e.g. AJAX call) then you need to wait until the input has been populated. E.g.
如果输入值由一个涉及一些延迟的脚本填充(例如AJAX调用),那么您需要等到输入已填充。例如。
var w = new WebDriverWait(WebBrowser, TimeSpan.FromSeconds(10));
w.Until((d) => {
// Wait until the input has a value...
var elements = d.FindElements(By.Name(name));
var ele = elements.SingleOrDefault();
if (ele != null)
{
// Found a single element
if (ele.GetAttribute("value") != "")
{
// We have a value now
return true;
}
}
return false;
});
var e = WebBrowser.Current.FindElement(By.Name(name));
if (e.GetAttribute("value") != value)
{
Assert.Fail("Result contains a field named '{0}', but its value is '{1}', not '{2}' as expected", name, e.GetAttribute("value"), value);
}
#7
0
This is kind of hacky, but it works.
这有点像hacky,但它确实有效。
I used JavascriptExecutor
and added a div
to the HTML, and changed the text in the div
to $('#prettyTime').val()
I then used Selenium to retrieve the div
and grab its value. After testing the correctness of the value, I removed the div that was just created.
我使用了JavascriptExecutor并在HTML中添加了div,并将div中的文本更改为$('#prettyTime')。val()然后我使用Selenium检索div并获取其值。在测试了值的正确性后,我删除了刚刚创建的div。
#8
0
For python bindings it will be :
对于python绑定,它将是:
element.get_attribute('value')