选择月份下拉列表中的值 - Selenium Webdriver

时间:2014-01-30 03:17:36

标签: java html selenium selenium-webdriver

enter image description here

目前正在开发Selenium Web驱动程序和Java。我想选择日期选择器形式的选项。我试图从日期选择器下拉菜单中选择月份。但它显示如下无法定位元素:{“方法”:“类名”,“选择器”:“ui-datepicker-month”} 命令持续时间或超时:31毫秒

所以我需要使用java脚本执行器使元素可见,但在下面的代码中我写成“document.getElementById('ui-datepicker-month'),所以它再次显示错误为”文档。 getElementById('')为空

这是HTML:

<div class="ui-datepicker-title">
<select class="ui-datepicker-month" onchange="DP_jQuery_1391048152193.datepicker._selectMonthYear('#fromDate', this, 'M');">
<option selected="selected" value="0">Jan</option>
<option value="1">Feb</option>
<option value="2">Mar</option>
<option value="3">Apr</option>
<option value="4">May</option>
<option value="5">Jun</option>
<option value="6">Jul</option>
<option value="7">Aug</option>
<option value="8">Sep</option>
<option value="9">Oct</option>
<option value="10">Nov</option>
<option value="11">Dec</option>
</select>

这是我试过的表格,但它不起作用:

JavascriptExecutor executor42 = (JavascriptExecutor)driver;
executor42.executeScript("document.getElementById('ui-datepicker-month').style.display='block';");
List<WebElement> select42 = new Select(driver.findElement(By.className("ui-datepicker-month"))).getOptions();
((Select) select42).selectByValue("Jun");

以下是堆栈跟踪:

失败:登录     显示java.lang.NullPointerException     在com.google.common.base.Preconditions.checkNotNull(Preconditions.java:192)     在org.openqa.selenium.support.ui.FluentWait。(FluentWait.java:94)     在org.openqa.selenium.support.ui.WebDriverWait。(WebDriverWait.java:66)     在org.openqa.selenium.support.ui.WebDriverWait。(WebDriverWait.java:40)     at test.OverviewAndEvolutionPR.performLogin(OverviewAndEvolutionPR.java:525)     at test.OverviewAndEvolutionPR.Login(OverviewAndEvolutionPR.java:76)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)     at java.lang.reflect.Method.invoke(Unknown Source)     at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:84)     在org.testng.internal.Invoker.invokeMethod(Invoker.java:714)     在org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)     at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)     at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)     at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)     在org.testng.TestRunner.privateRun(TestRunner.java:767)     在org.testng.TestRunner.run(TestRunner.java:617)     在org.testng.SuiteRunner.runTest(SuiteRunner.java:334)     在org.testng.SuiteRunner.run顺序(SuiteRunner.java:329)     在org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)     在org.testng.SuiteRunner.run(SuiteRunner.java:240)     在org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)     在org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)     at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)     在org.testng.TestNG.runSuitesLocally(TestNG.java:1149)     在org.testng.TestNG.run(TestNG.java:1057)     at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)     在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)     在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

3 个答案:

答案 0 :(得分:1)

首先,你的意思是“不工作”?什么都没发生?例外?请清楚解释一下。

我现在无法测试Java,但请调试以下内容:

// Why are you doing this? Can't you just click to open?
// I suspect this is why
JavascriptExecutor executor42 = (JavascriptExecutor)driver;
executor42.executeScript("document.getElementById('ui-datepicker-month').style.display='block';");

// if your code above works, then do the following
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-month")));

Select select42 = new Select(selectElement);
select42.selectByValue("5");
// or select42.selectByVisibleText("Jun");

如果您不想循环使用这些选项,请不要使用getOptions()并混淆List<WebElement>

请始终在API文档here中查找方法。

<强> selectByValue

  

选择具有与参数匹配的值的所有选项。也就是说,当给出“foo”时,这将选择如下选项:Bar

<强> selectByVisibleText

  

选择显示与参数匹配的文本的所有选项。也就是说,当给出“Bar”时,这将选择如下选项:Bar

答案 1 :(得分:1)

我使用QAF

解决方案
Select select = new Select(new QAFExtendedWebElement("your locator id"))
    .selectByVisibleText("your text from drop down");

答案 2 :(得分:0)

最后,我得到了整个日期选择器标签的答案。

以下是代码:

Log.info("Clicking on fromDate drop down");
driver.findElement(By.id("fromDate")).click();
WebElement selectElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-month")));
Select select42 = new Select(selectElement);
select42.selectByValue("5");
Thread.sleep(6000);

WebElement selectElement1 = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(By.className("ui-datepicker-year")));
Select select43 = new Select(selectElement1);
select43.selectByValue("2012");
Thread.sleep(6000);

WebElement dateWidget = driver.findElement(By.id(“ui-datepicker-div”));    List columns = dateWidget.findElements(By.tagName(“td”));

for (WebElement cell: columns){
//Select 13th Date 
if (cell.getText().equals("13")){
cell.findElement(By.linkText("13")).click();
break;
}
} 

感谢@ user1177636 ..