【问题标题】:How to get default value of element using placeholder in Protractor?如何在量角器中使用占位符获取元素的默认值?
【发布时间】:2019-10-10 14:23:35
【问题描述】:

如何以角度获取输入中存在的默认文本。

<input _ngcontent-c20="" 
autocomplete="off" type="text" disabled="true" 
placeholder="Username" class="ng-untouched ng-pristine ng-valid">

文本框中的默认值是“abc”。

如何得到这个值,使用getText()给出空白值。

PS:我们需要验证这些输入中存在的默认文本。

【问题讨论】:

  • 能否提供该输入界面元素的屏幕截图

标签: angular selenium protractor


【解决方案1】:

试试下面这个

ele = element(by.css('input[placeholder="Username"]'));

ele.getAttribute('value');  //This gets the input field value

希望对你有帮助

【讨论】:

  • @Rahul 请务必将答案标记为正确,以便帮助其他人解决错误。
【解决方案2】:

你可以用这个得到它:

document.querySelector('[placeholder="placeholder name"]')

尝试以下:

HTML:

<p>Click the button to get the value using placeholder of the text field.</p>

<input _ngcontent-c20="" 
autocomplete="off" type="text" disabled="true" value="abc"
placeholder="Username" class="ng-untouched ng-pristine ng-valid">

<button onclick="getText()">Try it</button>

<script>
    function getText() {
        var usernameInputElement = document.querySelector('[placeholder="Username"]')
        console.log(usernameInputElement.value); // Current input value
    }
</script>

console.log(usernameInputElement.value) 将打印出输入值。

【讨论】:

    【解决方案3】:

    使用 Javascript 执行器,您将获得“值”,这是输入元素的默认属性。

    以下函数将返回结果。

    async function getAttributeValueWithJS(attributeName: string, element: ElementFinder) {
        await browser.executeScript(`return arguments[0].getAttribute('${attributeName}')`, element);
    }
    
    const el = element(by.css('input[placeholder="Username"]'));
    const defValue = await getAttributeValueWithJS('value', el);
    

    【讨论】:

    • 有现成的方法getAttribute('value')。无需过于复杂
    • 默认的getAttribute func,它来自selenium impl,它与使用javascript执行器不同。如果值保存在 HTMLelement 内的不同元素属性中,您可能会得到空字符串
    猜你喜欢
    • 2019-02-23
    • 1970-01-01
    • 2013-11-23
    • 2017-04-16
    • 2014-02-20
    • 2012-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多