有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法从下拉列表中选择值:ElementNotVisibleException

我正在用Java selenium自动输入用户的数据,但他必须通过下拉菜单选择状态。我收到一个异常错误

我尝试过使用所有选择器:

drpState.selectByIndex(5);
drpState.selectByValue("CA");
drpState.selectByVisibleText("California");

我有以下html代码:

<div class="col-sm-4 form-group">
            <label id="xstateLabel">@lang('messages.state')</label>
            <select class="form-control js-select" style="width: 100%" id="xstate" name="xstate">
                <option value=""> State </option>
                <option value="AL" <?php if(isset($usr->state) && $usr->state=='AL'){echo 'selected';} ?>>Alabama</option>
                <option value="AK" <?php if(isset($usr->state) && $usr->state=='AK'){echo 'selected';} ?>>Alaska</option>
                <option value="AZ" <?php if(isset($usr->state) && $usr->state=='AZ'){echo 'selected';} ?>>Arizona</option>
                <option value="AR" <?php if(isset($usr->state) && $usr->state=='AR'){echo 'selected';} ?>>Arkansas</option>
                <option value="CA" <?php if(isset($usr->state) && $usr->state=='CA'){echo 'selected';} ?>>California</option>
                <option value="CO" <?php if(isset($usr->state) && $usr->state=='CO'){echo 'selected';} ?>>Colorado</option>
                <option value="CT" <?php if(isset($usr->state) && $usr->state=='CT'){echo 'selected';} ?>>Connecticut</option>
                *
                *
                *
          </select>

我创建了这个函数:

public void select() {
    Select drpState = new Select(driver.findElement(By.id("xstate")));

    drpState.selectByValue("CA");
}

但我得到了一个错误:

org.openqa.selenium.ElementNotVisibleException: element not interactable: Element is not currently visible and may not be manipulated
  (Session info: chrome=75.0.3770.100)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17763 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PBX-WS2-SL2', ip: '10.30.10.54', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_211'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, acceptSslCerts: false, applicationCacheEnabled: false, browserConnectionEnabled: false, browserName: chrome, chrome: {chromedriverVersion: 74.0.3729.6 (255758eccf3d24..., userDataDir: C:\Users\DAYANA~1.SIL\AppDa...}, cssSelectorsEnabled: true, databaseEnabled: false, goog:chromeOptions: {debuggerAddress: localhost:56255}, handlesAlerts: true, hasTouchScreen: false, javascriptEnabled: true, locationContextEnabled: true, mobileEmulationEnabled: false, nativeEvents: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), rotatable: false, setWindowRect: true, strictFileInteractability: false, takesHeapSnapshot: true, takesScreenshot: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unexpectedAlertBehaviour: ignore, unhandledPromptBehavior: ignore, version: 75.0.3770.100, webStorageEnabled: true}
Session ID: 7b5fe4de6ccaace4b79ddee37685d7a2

共 (1) 个答案

  1. # 1 楼答案

    试试这个

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("xstate")));
    
    Select drpState = new Select(driver.findElement(By.id("xstate")));
    
    drpState.selectByValue("CA");