首页 文章

使用Selenium Web Driver检索HTML输入的值

提问于
浏览
82

在webapp的HTML中,有以下代码

<input type="text" name="prettyTime" id="prettyTime" class="ui-state-disabled prettyTime"  readonly="readonly">

页面上实际显示的是显示时间的字符串 .

在Selenium Web Driver中,我有 WebElement 对象引用了 <input>

WebElement timeStamp = waitForElement(By.id("prettyTime"));

我想得到 WebElement 的值,换句话说,就是页面上打印的内容 . 我尝试了所有 WebElement getters,并没有检索到用户看到的实际值 . 有帮助吗?谢谢 .

8 回答

  • -1

    这有点hacky,但它确实有效 .

    我使用 JavascriptExecutor 并在HTML中添加了 div ,并将 div 中的文本更改为 $('#prettyTime').val() 然后使用Selenium检索 div 并获取其值 . 在测试了值的正确性后,我删除了刚刚创建的div .

  • 4

    试试 element.getAttribute("value")

    text 属性用于元素标记内的文本 . 对于输入元素,显示的文本不会被 <input> 标记包装,而是在 value 属性中 .

    注意:案件很重要 . 如果指定“值”,则会返回“null”值 . 这至少对C#来说是如此 .

  • 19

    你可以这样做:

    webelement time=driver.findElement(By.id("input_name")).getAttribute("value");
    

    这将为您提供在网页上显示的时间 .

  • 14

    用硒2,

    我通常这样写:

    WebElement element = driver.findElement(By.id("input_name"));
    String elementval = element.getAttribute("value");
    

    要么

    String elementval = driver.findElement(By.id("input_name")).getAttribute("value");
    
  • 151

    按照@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;
            }
        }
    

    它工作得很好,不会改变DOM

  • 1

    如前所述,你可以做那样的事情

    public String getVal(WebElement webElement) {
        JavascriptExecutor e = (JavascriptExecutor) driver;
        return (String) e.executeScript(String.format("return $('#%s').val();", webElement.getAttribute("id")));
    }
    

    但正如您所看到的,您的元素必须具有 id 属性,并且页面上还有jquery .

  • 5

    如果输入值由涉及某些延迟的脚本填充(例如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);
            }
    
  • 0

    对于python绑定,它将是:

    element.get_attribute('value')
    

相关问题