org.openqa.selenium.ElementNotVisibleException:chrome = 66.0.3359.139并且chromedriver = 2.36.540470不可见元素

时间:2018-06-14 11:19:50

标签: java selenium selenium-webdriver webdriver selenium-chromedriver

我必须将值设置为"总金额" field.But这个领域有规律  表达式,当我们输入值并移动到下一个字段时,  输入的值转换为" $ 1000.00"甲。这是1000给出的  示例

以下是关键字:

public boolean setValue(String LType, String Locator, String value, 
    String Expected) {

    By by = null;
    if (LType.length() < 2) {
        by = By(Locator);
    } else {
        by = By(LType, Locator);
    }

    WebElement element = null;

    try {
        if ((element = isElementPresent(by)) != null) {

            element.sendKeys("value",value);
            return true;
        }
    } catch (Exception e) {
        logger.error("Exception while SendKeys on element" + LType + " " + 
  Locator, e);
    }
    return false;
  }



public boolean isElementPresent(String LType, String Locator, String value, 
String Expected) {

    By by = null;
    if (LType.length() < 2) {
        by = By(Locator);
    } else {
        by = By(LType, Locator);
    }
    WebElement element = null;
    try {

        if ((isElementPresent(by)) != null) {

            return true;
        }
    } catch (WebDriverException e) {
        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0," + 
   element.getLocation().y + ")");
        element.click();
        return true;
    } catch (Exception e) {
        logger.error("Exception while isElementPresent element" + LType + " " 
 + Locator, e);
    }
    return false;
}

和HTML代码:这个Html代码有一些正则表达式

<div class="form-group">
            <label for="GrossAmount">Gross Amount</label>


<input class="form-control" data-val="true" data-val-number="The field Gross Amount must be a number." data-val-regex="Field accepts a positive number with a maximum of 17 digits including 2 decimals." data-val-regex-pattern="\d{0,15}(\.\d{1,2})?" id="GrossAmount" name="GrossAmount" style="display: none;" type="text" value="">
<input class="form-control" id="GrossAmount_C" name="GrossAmount.C" style="display: block;" type="text" value="">
<script type="text/javascript">
    jQuery(document).ready(function ($) {
        $('#GrossAmount').focusout(function () { CurrencyFocusOut('GrossAmount_C', this) });
        $('#GrossAmount_C').focus(function () { CurrencyResultFocus('GrossAmount', this) });
        CurrencyKeyPress('#GrossAmount');
    });
</script>
            <span class="field-validation-valid text-danger" data-valmsg-for="GrossAmount" data-valmsg-replace="true"></span>
        </div>


<!-- end snippet -->

代码在调试模式下工作正常,并在运行模式下设置值  不工作

错误堆栈跟踪:

ERROR : 2018-06-14 18:12:33 : Driver.setNum():746 - Exception while SendKeys on element //input[@id='GrossAmount_C'] org.openqa.selenium.ElementNotVisibleException: element not visible (Session info: chrome=66.0.3359.139) (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91)

请求您提供帮助。

2 个答案:

答案 0 :(得分:0)

您可以尝试点击并在焦点元素上输入值,如下所示。

driver.findElement(By.id("GrossAmount_C")).click();
driver.switchTo().activeElement().sendKeys("1000");

driver.switchTo().activeElement().sendKeys(Keys.chord("1000"));

答案 1 :(得分:0)

此错误消息...

org.openqa.selenium.WebDriverException: unknown error: cannot focus element (Session info: chrome=66.0.3359.139) (Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91)

...表示 ChromeDriver v2.36 无法启动/产生新的 WebBrowser ,即 Chrome浏览器v66.0 会话

您的主要问题是所使用的二进制版本之间的不兼容性

  • 您正在使用 chromedriver = 2.36
  • chromedriver=2.36的发行说明中明确提到以下内容:
  

支持 Chrome v63-65

  • 您正在使用 chrome = 66.0
  • ChromeDriver v2.40的发行说明中明确提到以下内容:
  

支持 Chrome v66-68

  • 您的 Selenium Client 版本对我们未知

因此 ChromeDriver v2.36 Chrome浏览器v66.0

之间存在明显的不匹配

解决方案