根据答案进行调查,其中某些字段保持隐藏

时间:2018-07-13 19:41:00

标签: java selenium selenium-webdriver css-selectors ui-automation

我正在尝试使调查自动化。它是肯定的,没有问题。根据答案,有些问题仍然隐藏。

这是“否”按钮

                <label class="btn btn-primary ng-pristine ng-untouched ng-empty ng-valid ng-valid-required" ng-model="question.answer" ng-required="question.isOptional == false" uib-btn-radio="false" ng-change="clearIsValid(question);ifHasDependency(question,'BOOLEAN',$index);">No</label>
            </div>"

我尝试过:

for (int i = 17; i <= 100; i++) {

        for(int i=1;i<=200;i++ ){
            Thread.sleep(1000);
            if (driver.findElement(By.xpath("//*[@id=\"ngdialog2\"]/div[2]/span/wizard/div/div[4]/div/div/wizard-step["+i+"]/ng-form/ng-transclude/div[2]/div/div/label[1]")).isDisplayed()){

                driver.findElement(By.xpath("//*[@id=\"ngdialog2\"]/div[2]/span/wizard/div/div[4]/div/div/wizard-step["+i+"]/ng-form/ng-transclude/div[2]/div/div/label[1]")).click();
            }else{
                System.out.println("Question number " + i + " is missing.");
            }
        }

结果是:

org.openqa.selenium.ElementNotVisibleException: element not visible

请帮助:) 谢谢

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

for (int i = 17; i <= 100; i++) {
    for(int i = 1; i <= 200; i++){
        Thread.sleep(1000);
        // locate element
        WebElement element = driver.findElement(By.xpath("//*[@id=\"ngdialog2\"]/div[2]/span/wizard/div/div[4]/div/div/wizard-step["+i+"]/ng-form/ng-transclude/div[2]/div/div/label[1]"));
        try {
            // wait until element will be clickable
            new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(element));
            element.click(); // click on element
        }catch (Exception e){
            System.out.println("Question number " + i + " is missing.");
        }
    }
}

注意:您必须进行一些导入:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

PS:如elementToBeClickable()的源代码所述:

  public static ExpectedCondition<WebElement> elementToBeClickable(final WebElement element) {
    return new ExpectedCondition() {
      public ExpectedCondition<WebElement> visibilityOfElement = ExpectedConditions.visibilityOf(element);

      public WebElement apply(WebDriver driver) {
        WebElement elementx = (WebElement)this.visibilityOfElement.apply(driver);

        try {
          return elementx != null && elementx.isEnabled()?elementx:null;
        } catch (StaleElementReferenceException var4) {
          return null;
        }
      }

      public String toString() {
        return "element to be clickable: " + element;
      }
    };
  }

将在可见性以及启用了元素的情况下证明该元素。换句话说,如果该元素准备好接收,请单击它。