文本在页面上可见,但在 html 中不作为值或任何属性(Angular 项目)显示。如何使用硒或量角器获得价值

Text is visible on page but not in html as value or any attribute (Angular project). How to get value using selenium or protractor

<input _ngcontent-c16="" class="form-control ng-valid ng-dirty ng-touched" formcontrolname="email" id="input-email" type="text" ng-reflect-klass="form-control" ng-reflect-ng-class="[object Object]" ng-reflect-name="email" placeholder="Email">

如何获取电子邮件输入值?

https://i.stack.imgur.com/ea9Zj.png

要查找输入的值或在某些输入中键入的任何内容,请使用 getValue() 方法

String text = driver.findElement(By.id("input-email")).getValue();

在 angular 中,您可以通过几种方式将数据从代码绑定到模板。

  1. 模板驱动的表单 - [(ngModel)]

component.ts

email: string;

component.html

<input type="text" class="form-control" id="email" required [(ngModel)]="email" />

Check this for template driven forms

  1. 反应形式

Component.ts

myForm: any;

constructor(
    private formBuilder: FormBuilder,
){
    this.ribbonForm = this.formBuilder.group
   ({
      email: ['', Validators.required]
   });
}

OnSubmit(form){
   //any thing you can do with form data
}

component.html

<form [formGroup] = "myForm" (ngSubmit)="OnSubmit(myForm)">
    <input type="email" formControlName="email"/>
    <button type="submit">Submit</button>
</form>

Check this for reactive forms

你可以这样做:

const emailValue = await $("input#input-email").getText();

或者如果不在 async 函数内:

const emailValue = $("input#input-email").getText().then((text) => text);

试试下面的选项

var ele = element(by.id('input-email'));

var inputText = ele.evaluate(ele.getAttribute('value'));  // Returns the text

var inputText = ele.getAttribute('value'); //returns the text

如果您没有在 html 中找到文本,您可以尝试以下方法:

WebElement element = driver.findElement(By.id("input-email"));
var text = element.getAttribute("innertext");

或者:

element.getAttribute("value");