Question: I am a bit new to automation and i am trying to write a script to fill few data in a particular screen and submit them. Navigation to the screen and adding data to few fields are done properly. 

The screen has two datepickers. The **"From date"** one gets picked and date is been posted but not the **"To date"** picker.

[![Screen view](https://i.stack.imgur.com/4Cw06.png)](https://i.stack.imgur.com/4Cw06.png)

Can someone please help me.

This is the code I have developed.

```
public class BusinessCalendar extends Login{

static String Nav = "xxxx";

    @BeforeTest
    public void Navigation(){
        driver.navigate().to(Nav);
    }

    @Test
    public void CreateBusinessCalendar(){
        driver.findElement(By.id("txtBusinessYear")).click();
        driver.findElement(By.id("txtBusinessYear")).clear();
        driver.findElement(By.id("txtBusinessYear")).sendKeys("test year");
        driver.findElement(By.id("dtpFromDates")).clear();   
        driver.findElement(By.xpath("/html/body/div[10]/div[1]/table/tbody/tr[2]/td[5]")).click();
        driver.findElement(By.id("dtpToDates")).clear();
        driver.findElement(By.xpath("/html/body/div[10]/div[1]/table/tbody/tr[3]/td[4]")).click();
    }

    @AfterTest
    public void CloseBrowser(){
        driver.close();
        driver.quit();
    }
}

```

Answer: 
Place the following code in your Experiment JavaScript:

```
setCookie = function (c_name,value,exdays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate() + exdays);
  var expires = exdate.toUTCString();
  var isIE8 = (document.documentMode !== undefined);
  if (exdays == 0) {
      expires = (isIE8 == true) ? "" : "0";
  }
  var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+expires);
  document.cookie=c_name + "=" + c_value;
}

```

Within each variant (e.g. Blue Variant, Red Variant, etc.), call setCookie and check whether the cookie exists for a user in that particular variant. If the cookie does exist, then run whatever code you want that particular variant to run. Below is an example of setting a cookie called tester that will expire after 30 days.

```
setCookie('tester',true,30);

if(document.cookie.indexOf('tester')>-1){
  //RUN YOUR CODE HERE IF THIS VISITOR HAS THIS COOKIE
}

```
Also keep in mind that Optimizely gets blocked by adblockers, it can happen if a user turns their adblocker on after they've been on the page and served up an experiment to see - they would get the original version.