0

I am trying to click on a date picker text box so that a calendar pops up where I can choose the date.

I am able to identify the element since it has easy accessible "id" attribute.

Manually clicking on the textbox, results in the calendar pop being displayed.

However, I am not able to click on the date picker using native Selenium click commands/ Javascript/ Jquery clicks.

Below is the HTML code :

<input class="ng-touched ng-dirty ng-valid" formcontrolname="effectiveDate" id="date_start" name="date_start" placeholder="dd/mm/yyyy" readonly="" type="text" ng-reflect-name="effectiveDate" autocomplete="off" value="22/09/2017" ng-reflect-model="22/09/2017">

Any reason why such a behavior? In what instances can we expect the Selenium native commands/ JS/ Jquery command to fail?

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
Ajmal Moideen
  • 170
  • 1
  • 6
  • 17

4 Answers4

0

Using JavascriptExecutor You can enter date..

 ((JavascriptExecutor) wd).executeScript(
                    "document.getElementsByClassName('ng-touched ng-dirty ng-valid')[0].setAttribute('value', '2017-10-21')");
Zakaria Shahed
  • 2,589
  • 6
  • 23
  • 52
  • Thanks for the response. This solution does set the text box value with the input that we need. However, there are some back-end calls happening when you do a mouse click on the date-picker according to which further fields in the form will be displayed. So, this solution doesn't bring in the hidden fields. – Ajmal Moideen Sep 22 '17 at 12:17
0

As per code this is written in angularjs so selenium driver is not able to locate such type of elements.Try to use cssSelector which is the best approach to locate angular based elements you can use By.cssSelector("input [id=date_start]").Don't forget to use proper wait before locating element.

Indrapal Singh
  • 364
  • 1
  • 2
  • 13
  • Thanks for the response. I tried to identify the element using JS as well. The element identification works well in Selenium WD, JS commands. However, the click operation doesn't work. I did also try adding appropriate wait statements. – Ajmal Moideen Sep 22 '17 at 12:16
0

You can try any of the following approaches:

#1

WebElement datePicker = driver.findElement(By.id("date_start"));

WebDriverWait wait = new WebDriverWait(driver,20);
wait.until(ExpectedConditions.visibilityOf(datePicker));
wait.until(ExpectedConditions.elementToBeClickable(datePicker));
datePicker.click();

#2

Actions act = new Actions(driver);
act.moveToElement(datePicker).click().build().perform();

#3

JavascriptExecutor js= (JavascriptExecutor ) driver;
js.executeScript("arguments[0].click‌​();",datePicker);
Kushal Bhalaik
  • 3,349
  • 5
  • 23
  • 46
  • I have tried the method 1 and method 3. It still doesn't work. I did try with method #2 with `act.moveToElement(datePicker).click().build().perform();` . How is it different from `moveTopElement(datePicker)`? – Ajmal Moideen Sep 25 '17 at 04:54
  • Sorry its not `moveTopElement` Its `moveToElement` – Kushal Bhalaik Sep 25 '17 at 05:16
  • what is the outcome that you get when you try any of these methods? – Kushal Bhalaik Sep 25 '17 at 05:17
  • I don't get any exception when trying these methods. It's just that it skips to the next command. This would mean the command is getting executed properly without error . But, the click operation is not happening in the application. – Ajmal Moideen Sep 25 '17 at 05:29
  • is it possible for you to share the url? – Kushal Bhalaik Sep 25 '17 at 05:30
  • Unfortunately, I won't be able to do that. Again, what are the situations that you have across where the native Selenium click operation fails? – Ajmal Moideen Sep 25 '17 at 07:40
  • Mostly it is when element is not visible at the moment. In you case it is because of angular code.. which takes time to load. I'd suggest better look at [this](https://stackoverflow.com/questions/28057338/understanding-execute-async-script-in-selenium) – Kushal Bhalaik Sep 25 '17 at 09:15
-1

I think you will have to take coordinates of text box and width and height of the textbox and try to click on centre of the calendar sign present in textbox...

Ajay
  • 1