Solved: Firefox Datepicker Sendkey Problem -different to Chrome -C# Selenium

Viewed 41

I have a working Code to send a Date to a Datepicker with Sendkeys for Browser Chrome and Edge. But this Code will not work in Browser Firefox ...

// working in Chrome and Edge

DateTime Datum_now = DateTime.Now;
string str_Datum_now;
str_Datum_now = Datum_back.ToString("ddMMyyyy", CultureInfo.InvariantCulture);

webdriver.FindElement(By.Id("start-date")).Click();
webdriver.FindElement(By.Id("start-date")).SendKeys(str_Datum_now);

Firefox is handling this Datepicker different, as a User get an PopUp with a Calendar. I tried to close this additional Calendar by sending an -Escape- or a -Right-Click- But this will not change anything in the Situation. Sendkey will not work.

Here are my not working modifications in trying to get this work for Firefox:

// Firefox modifications
// Try 1:

DateTime Datum_now = DateTime.Now;
string str_Datum_now;
str_Datum_now = Datum_back.ToString("ddMMyyyy", CultureInfo.InvariantCulture);

webdriver.FindElement(By.Id("start-date")).Click();

// Right Click
Actions actions = new Actions(webdriver);
IWebElement elementLocator = webdriver.FindElement(By.Id("start-date"));
actions.ContextClick(elementLocator).Perform();

webdriver.FindElement(By.Id("start-date")).SendKeys(str_Datum_now);
// Firefox modifications
// Try 2:
DateTime Datum_now = DateTime.Now;
string str_Datum_now;
str_Datum_now = Datum_back.ToString("ddMMyyyy", CultureInfo.InvariantCulture);

//webdriver.FindElement(By.Id("start-date")).Click();

// Right Click
Actions actions = new Actions(webdriver);
IWebElement elementLocator = webdriver.FindElement(By.Id("start-date"));
actions.ContextClick(elementLocator).Perform();

webdriver.FindElement(By.Id("start-date")).SendKeys(str_Datum_now);
// Firefox modifications
// Try 3:
DateTime Datum_now = DateTime.Now;
string str_Datum_now;
str_Datum_now = Datum_back.ToString("ddMMyyyy", CultureInfo.InvariantCulture);

webdriver.FindElement(By.Id("start-date")).Click();

Actions action = new Actions(webdriver);
action.SendKeys(OpenQA.Selenium.Keys.Escape);

webdriver.FindElement(By.Id("start-date")).SendKeys(str_Datum_now);

The Webside i am talking about is: https://eas.forexsb.com/

On the top go to "Data" and down there to "Data Horizon". Both of the Date-Field will have the same situation. In my example i take "Use start date limit" Date.

Did you have any idea to solf this problem? How to send the Date by using Sendkeys?


Edit: I found a working solution! That will do the trick for Firefox, Chrome and Edge:

IJavaScriptExecutor js1 = (IJavaScriptExecutor)webdriver;
js1.ExecuteScript("document.getElementById('start-date').value='2012-03-31'");
0 Answers
Related