如何找到一个文本字段的xpath和填充数据?



我正在用java测试selenium应用程序。并试图找到一个文本字段的xpath。

任何帮助,谢谢你

<div class="mat-form-field">
<input matinput class="mat-input-element mat-form-field-autofill-monitored" required id="mat-input-5" name="threshold-2" aria-required="true" aria-invalid="false">

我试着:

xpath = ("//div[@class='mat-form-field']")
xpath = ("//div[@id='mat-input-5']")
xpath = ("//div[@id='mat-input-element mat-form-field-autofill-monitored']")

都不适合我。收到的错误是:没有这样的元素:无法定位元素

文本字段还有一个文本标签,它是:

<label class='mat-form-field-label ng-tn>' id='mat-form-field-label-100' for='mat-input-10' aria-owns='mat-input-20'>
<!---->
<mat-label class='ng-tns-c87 ng-star-inserted'>Enter value here please<mat-label class/>

所以我试了:xpath = "//div[contains(text(), 'Enter value here please']"

收到的错误是:没有这样的元素:无法定位元素

试试这个XPATH表达式:

//input[@id='mat-input-5']

或:

//input[@name='threshold-2']

  1. 首先检查您是否可以手动检查浏览器并使用上面的XPATH表达式找到元素,如果是,
  2. 尝试应用waits。参见下面的代码:
new WebDriverWait(driver, Duration.ofSeconds(20)).until(
ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@id='mat-input-5']"))).click();
  1. 即使在此之后,如果你得到Unable to locate element,然后检查IFRAME的存在,如果有IFRAME,你需要首先将上下文切换到IFRAME如果有帮助,请参考此链接:https://stackoverflow.com/a/75865161/7598774

最新更新