1
votes

enter image description here

Currently working on Selenium web driver and Java. I want to select the option which in the form of date picker. I tried to select the month from the date picker drop down. but it is showing follows Unable to locate element: {"method":"class name","selector":"ui-datepicker-month"} Command duration or timeout: 31 milliseconds

So i need to make visible the element using java script executor but in the below code i have written as "document.getElementById('ui-datepicker-month') so again it is showing error as "document.getElementById('') is null

Here is the 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>

Here is the form i tried but it is not working:

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");

Below are the stacktrace:

FAILED: Login java.lang.NullPointerException at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:192) at org.openqa.selenium.support.ui.FluentWait.(FluentWait.java:94) at org.openqa.selenium.support.ui.WebDriverWait.(WebDriverWait.java:66) at 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) at org.testng.internal.Invoker.invokeMethod(Invoker.java:714) at 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) at org.testng.TestRunner.privateRun(TestRunner.java:767) at org.testng.TestRunner.run(TestRunner.java:617) at org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at org.testng.SuiteRunner.run(SuiteRunner.java:240) at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at org.testng.TestNG.run(TestNG.java:1057) at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

3

3 Answers

1
votes

First, what do you mean "not working"? Nothing happens? Exceptions? Explain it clearly please.

I can't test Java now, but please debug the following:

// 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");

If you don't want to loop through the options, don't use getOptions() and mess with List<WebElement>.

Please always look up methods in the API doc here.

selectByValue

Select all options that have a value matching the argument. That is, when given "foo" this would select an option like: Bar

selectByVisibleText

Select all options that display text matching the argument. That is, when given "Bar" this would select an option like: Bar

1
votes

I have solution using QAF

Select select = new Select(new QAFExtendedWebElement("your locator id"))
    .selectByVisibleText("your text from drop down");
0
votes

Finally i got the answer for the entire date picker tab.

Here is the code:

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;
}
} 

Thanks to @ user1177636..