使用 Selenium Web Driver 检索 HTML 输入的值 [英] Using Selenium Web Driver to retrieve value of a HTML input
问题描述
在 webapp 的 HTML 中有以下代码
In the HTML of a webapp there is the following code
<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.
在 Selenium Web Driver 中,我有一个 WebElement
对象引用 使用
In Selenium Web Driver, I have a WebElement
object referring to the <input>
using
WebElement timeStamp = waitForElement(By.id("prettyTime"));
我想获取 WebElement
的值,或者换句话说,页面上打印的内容.我尝试了所有 WebElement
获取器,但没有检索到用户看到的实际值.有什么帮助吗?谢谢.
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.
推荐答案
这有点笨拙,但确实有效.
This is kind of hacky, but it works.
我使用了 JavascriptExecutor
并在 HTML 中添加了一个 div
,并将 div
中的文本更改为 $('#PrettyTime').val()
然后我使用 Selenium 来检索 div
并获取它的值.在测试了值的正确性后,我把刚刚创建的div去掉了.
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.
这篇关于使用 Selenium Web Driver 检索 HTML 输入的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!