简体   繁体   中英

How to extract the value when there is no value tag using Selenium and Java

I want to get value of input tag and compare with sum of 2 numbers. But there is no value tag, or I can not see result number. I tried to get but I got null. The picture and Input tag is like under.

<input _ngcontent-lbg-c92="" matinput="" type="number" class="mat-input-element mat-form-field-autofill-control ng-tns-c35-2 cdk-text-field-autofill-monitored ng-untouched ng-pristine" style="text-align: center;" id="mat-input-2" aria-invalid="false" aria-required="false" disabled="">

Element snapshot:

快照

To print the value ie 1626 you can use either of the following locator strategies :

  • Using cssSelector :

     System.out.println(driver.findElement(By.cssSelector("input.mat-input-element[id^='mat-input'][type='number'][matinput]")).getAttribute("value"));
  • Using xpath :

     System.out.println(driver.findElement(By.xpath("//input[contains(@class, 'mat-input-element') and starts-with(@id, 'mat-input')][@type='number' and @matinput]")).getAttribute("value"));

However, as the element is a dynamic element so to identify the element you need to induce WebDriverWait for the presenceOfElementLocated() and you can use either of the following locator strategies :

  • Using cssSelector :

     System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("input[id*='Empty'][value='emptyAssembly']"))).getAttribute("value"));
  • Using xpath :

     System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//input[@value='emptyAssembly' and contains(@id, 'Empty')]"))).getAttribute("value"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM