java - 如何使用 Java 编写 Selenium Web 驱动程序中复选框的脚本

标签 java selenium cucumber

这是我想要单击的按钮。我想通过使用 Java 在 selenium Web 驱动程序中编写自动化脚本来检查此按钮。

以下是按钮的代码。

<label class="toggle" style="font-size:13px">
<input type="checkbox" name="checkbox-IsEFR" id="checkbox-IsEFR">
<i data-swchon-text="Yes" data-swchoff-text="No"></i>Is Enable for Rules
</label>

请帮助我,因为我是自动化脚本编写的新手。我为复选框编写了脚本,但它不起作用。下面,我附上了我的脚本和生成的错误。

@When("^clicks on enable for rules$")
    public void clicks_on_enable_for_rules() throws Throwable {
        driver.findElement(By.name("checkbox-IsEFR")).click();
    }

我收到的错误。

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Command duration or timeout: 209 milliseconds Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58' System info: host: 'DELL_I5', ip: '192.168.1.33', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77' Driver info: org.openqa.selenium.firefox.FirefoxDriver Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=46.0, platform=WINDOWS, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}] Session ID: 03695ef8-bd24-49d6-b8a1-6687e3d0375c at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) at java.lang.reflect.Constructor.newInstance(Unknown Source) at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85) at mCollector.features.StepDefinitions_mCollector.clicks_on_enable_for_rules(StepDefinitions_mCollector.java:61) at ✽.And clicks on enable for rules(C:/Users/Admin/workspace/MStudio - eBilling/src/mCollector/features/mCollector.feature:12) Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with Build info: version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'DELL_I5', ip: '192.168.1.33', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'
Driver info: driver.version: unknown at .fxdriver.preconditions.visible(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:10092) at .DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12644) at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661) at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12666) at .DelayedCommand.prototype.execute/<(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)

最佳答案

您可以使用显式等待和 ExpectedConditions等待复选框可见

WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("checkbox-IsEFR"))).click();

在单击该元素之前,将等待最多 10 秒以使该元素可见。

关于java - 如何使用 Java 编写 Selenium Web 驱动程序中复选框的脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37019292/

相关文章:

java - http 客户端 -> 发送的消息包含一个 & 符号

java - 使用随机生成两个单独的输出时遇到问题

java - 如何在 Selenium WebDriver 中获取 "li"类的所有 "ul"元素

tags - ( cucumber )Background和Before标签有什么区别

java - Android Parcelable - 当 IA 为接口(interface)时写入和读取 ArrayList<IA>

java - 在 html 页面内显示内容类型为 octet-stream 的 url 中的文件

python - 如何在 Python + Selenium 中获取元素的值

java - 构造“try/catch”不会捕获异常“NoSuchElementException”

ruby-on-rails - cucumber 和 Rspec

ruby-on-rails - 没有要加载的此类文件-运行 cucumber 时zip/zip(MissingSourceFile)