【问题标题】:How to write the script for the check box in Selenium web driver by using Java如何使用 Java 编写 Selenium Web 驱动程序中复选框的脚本
【发布时间】:2016-05-04 05:36:08
【问题描述】:

这是我想要点击的按钮。我想通过使用 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: 元素不是 当前可见,因此可能无法与命令持续时间交互 或超时:209 毫秒构建信息:版本:'2.53.0', 修订:'35ae25b',时间:'2016-03-15 17:00:58' 系统信息:主机: 'DELL_I5', ip: '192.168.1.33', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77' 驱动信息: org.openqa.selenium.firefox.FirefoxDriver 功能 [{applicationCacheEnabled=true,rotatable=false,handlesAlerts=true, databaseEnabled=true,版本=46.0,平台=WINDOWS, nativeEvents=false,acceptSslCerts=true,webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, 需要Screenshot=true,javascriptEnabled=true, cssSelectorsEnabled=true}] 会话 ID: 03695ef8-bd24-49d6-b8a1-6687e3d0375c 在 sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法) 在 sun.reflect.NativeConstructorAccessorImpl.newInstance(未知 来源)在 sun.reflect.DelegatingConstructorAccessorImpl.newInstance(未知 来源)在 java.lang.reflect.Constructor.newInstance(未知 来源)在 org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206) 在 org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158) 在 org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678) 在 org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327) 在 org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85) 在 mCollector.features.StepDefinitions_mCollector.clicks_on_enable_for_rules(StepDefinitions_mCollector.java:61) 在✽。然后单击启用规则(C:/Users/Admin/workspace/MStudio - eBilling/src/mCollector/features/mCollector.feature:12) 原因:org.openqa.selenium.ElementNotVisibleException:元素不是 当前可见,因此可能无法与构建信息交互: 版本:'2.53.0',修订:'35ae25b',时间:'2016-03-15 17:00:58'
系统信息:主机:'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.version:在 .fxdriver.preconditions.visible(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-处理器.js:10092) 在 .DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12644) 在 .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12661) 在 .DelayedCommand.prototype.executeInternal_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver@googlecode.com/components/command-processor.js:12666) 在 .DelayedCommand.prototype.execute/

【问题讨论】:

  • 尝试 driver.findElement(By.Id("checkbox-IsEFR")).click();
  • 您是否在测试用例中添加了隐式等待,例如 driver.manage().timeouts().implicitWait(15,TimeUnit.SECONDS);

标签: java selenium cucumber


【解决方案1】:

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

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

这将等待最多 10 秒以使元素可见,然后再单击它。

【讨论】:

  • 它也会产生错误。错误如下。 org.openqa.selenium.TimeoutException:等待 By.id 定位的元素可见性 10 秒后超时:checkbox-IsEFR 构建信息:版本:'2.53.0',修订:'35ae25b',时间:'2016-03 -15 17:00:58' 系统信息:主机:'DELL_I5',ip:'192.168.1.33',os.name:'Windows 10',os.arch:'amd64',os.version:'10.0', java.version: '1.8.0_77'
  • @ChathurikaPrabodani 复选框有什么东西吗?
  • 我不清楚。你能描述一下你在问什么。实际上按钮没有与任何部分组合。它用于启用规则或不启用规则为是或否。
  • @ChathurikaPrabodani 我在问您是否可以看到该复选框,或者它是否隐藏在其他东西后面/您需要滚动才能看到它。
  • Setup -> Attributes -> 然后是带有这个 yes no 复选框的表单。
【解决方案2】:

错误是:“ElementNotVisibleException:元素当前不可见,因此可能无法与之交互..”

发生此错误的原因可能有多种,但最可能的原因是:

该元素未加载到页面上,您的代码正在尝试单击它。要处理此问题,您可以在代码中放置等待语句。可以通过设置隐式等待driver.manage().timeouts().implicitlyWait(5000L, TimeUnit.SECONDS); 来强制执行全局等待,或者您可以尝试在执行点击活动之前设置显式等待语句。

尝试解决方案,我认为它会解决您的问题。

【讨论】:

    【解决方案3】:

    您可以在 Selenium 中通过 xpath 进行搜索,使用类似:

    driver.find_element_by_xpath(".//*[contains(text(), 'Is Enable for Rules')]").click()

    【讨论】:

      猜你喜欢
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-30
      相关资源
      最近更新 更多