[英]Selenium not able to select element by XPath based on attributes
我有一個需要查找和測試的輸入/文本框。 通過檢查瀏覽器上的元素,我發現該元素為:
<input _ngcontent-tlw-c35="" autocomplete="off"
class="mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-dirty ng-valid ng-touched"
matinput="" type="text"
ng-reflect-required="true"
ng-reflect-autocomplete="[object Object]"
ng-reflect-autocomplete-attribute="off"
ng-reflect-name="provider"
ng-reflect-placeholder="Provider"
ng-reflect-type="text" required="" id="mat-input-71" placeholder="Provider" aria-invalid="false" aria-required="true" aria-describedby="mat-hint-20 mat-hint-21" xpath="1" style="">
出於某種原因,我需要通過 xpath 找到這個元素(我使用的是 java)。 因此我嘗試了:
WebElement provider = super.wait(WAIT, INTERVAL).until(driver -> driver.findElement(By.xpath("//input[@class='mat-input-element' and @ng-reflect-placeholder='provider']")));
得到錯誤:
Error Message: org.openqa.selenium.TimeoutException: Supplied function might have stalled
Build info: version: '4.0.0-alpha-6', revision: '5f43a29cfc'
System info: host: '38022f96913d', ip: '172.23.0.12', os.name: 'Linux', os.arch: 'amd64', os.version: '4.19.76-linuxkit', java.version: '11.0.9.1'
Driver info: driver.version: unknown
Stacktrace:
org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:221)
關於我做錯了什么的任何見解?
你離得夠近了。 您已將class屬性的值用作mat-input-element
,其中class的值是mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-dirty ng-valid ng-touched
您可以使用以下任一定位器策略:
在lambda表達式中使用單個class屬性:
WebElement provider = super.wait(WAIT, INTERVAL).until(driver -> driver.findElement(By.xpath("//input[contains(@class, 'mat-input-element') and @ng-reflect-placeholder='Provider']")));
通過WebDriverWait使用class屬性:
WebElement provider = super.wait(WAIT, INTERVAL).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@class='mat-input-element mat-form-field-autofill-control cdk-text-field-autofill-monitored ng-dirty ng-valid ng-touched' and @ng-reflect-placeholder='Provider']")));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.