Java Selenium Element目前无法进行交互,可能无法操作

时间:2017-02-15 07:16:12

标签: java selenium selenium-chromedriver

这是我的代码:

click(By.id("job-description-help"), MOUSE_LEFT, 2);
click(By.id("job-description-help"), MOUSE_LEFT, 2);
click(By.id("title"), MOUSE_LEFT, 2);
sendKeys(By.id("title"), "ttttt", true, 4);
click(By.xpath(".//*[@id='editor']/span[5]/a[1]"), MOUSE_LEFT, 7);

    public void click(By locator, int mouseButton, String combinedControlKeys, int timeoutInSeconds) throws Exception {
    Boolean success = false;
    long timeoutInMillies = timeoutInSeconds * 1000;
    WebElement elm = null;
    // indicate whether the element is an <option> tag on Firefox browser
    Boolean isOptionTagOnFireFox = false;
    // time spent counters
    long timeSpentInMillis = 0;
    // try to click on the element until wait timout reached
    while ((success == false) && (timeSpentInMillis <= timeoutInMillies)) {
      try {
        // find the element and calculate time spent
        long startFinding = System.currentTimeMillis();
        elm = findElement(locator, 1);
        timeSpentInMillis += System.currentTimeMillis() - startFinding;

        if (elm != null) {
          // get tag name of the element
          String tag = elm.getTagName();
          // check whether the element is <option> tag on Firefox browser
          isOptionTagOnFireFox = isFirefoxDriver && (0 == elm.getTagName().compareToIgnoreCase("option"));

          // press control keys (Ctrl, Alt, Shift, Meta), if any
          pressControlKeys(combinedControlKeys);
          if (mouseButton == MOUSE_LEFT) {
            // click on the element
            elm.click();  
          }
          else if (mouseButton == MOUSE_RIGHT) {
            Actions action = new Actions(driver);
            action.contextClick(elm);
            action.build().perform();
          }
          // release control keys (Ctrl, Alt, Shift, Meta)
          releaseControlKeys(combinedControlKeys);
          // successfully clicked on element, turn on the flag to escape the loop
          success = true;
        }
      } catch (Exception e) { 
        releaseControlKeys(combinedControlKeys);
        // probably the element is not present yet or it is non-clickable
        System.out.println(e.getMessage());
        // if element is found and we reached to the timeout, try to click on it with JavaScript
        if ((elm != null) && (timeSpentInMillis > timeoutInMillies)) {
          // press control keys (Ctrl, Alt, Shift, Meta), if any
          pressControlKeys(combinedControlKeys);
          JavascriptExecutor js = (JavascriptExecutor) this.driver;
          js.executeScript("arguments[0].click();", elm);
          releaseControlKeys(combinedControlKeys);
          // successfully clicked on element, turn on the flag to escape the loop
          success = true;
        }
      }
    }

    if (success) {
      // if element is <option> tag on Firefox browser,
      // perform a workaround to support MOUSE_LEFT
      if (isOptionTagOnFireFox)
      {
        robot.keyPress(KeyEvent.VK_ENTER);
        robot.delay(300);
        robot.keyRelease(KeyEvent.VK_ENTER);
      }
    }
    else { // failed
      if (elm == null) {
          throw new NoSuchElementException("Element not found.");
      } else {
          throw new Exception("Failed to click on the element");
      }
    }
  }

    public void sendKeys(By locator, String keys, boolean clearTextBeforeSendingKeys, int timeoutInSeconds) throws Exception {
    WebElement elm = findElement(locator, timeoutInSeconds);
    if (clearTextBeforeSendingKeys) elm.clear();
    elm.sendKeys(keys);
  }

错误

  

无效元素状态:元素当前不可交互且可能无法操作

开始
sendKeys(By.id("title"), "ttttt", true, 4);

我正在使用Eclipse和ChromeDriver 2.27

不确定原因,但输入字段可见。由于错误无法前进。任何想法如何解决这个错误?

0 个答案:

没有答案