How to select a value from Calendar using Selenium Webdriver

On the ticket reservation website www.redbus.in I need to choose the date of the trip ...

How will this be done with Selenium WebDriver?

0
source share
4 answers

Just try using xpath

String month="Sept"; String date="28"; "//td[text()='"+month+"']/../..//a[text()='"+date+"']" 

He will choose September 28 enter image description here

Indicate the month and date based on your requirements.

The logic below is for navigating between months.

 driver.findElement(By.cssSelector("td.next")).click(); driver.findElement(By.cssSelector("td.previous")).click(); 

enter image description here

I hope this works (I have not tried it on my machine)

EDIT-I

I tried the logic in my machine and it works fine.

 driver=new FirefoxDriver(); driver.get("http://www.redbus.in"); //selecting date of journey driver.findElement(By.id("calendar")).click(); driver.findElement(By.xpath("//td[text()='Sept']/../..//a[text()='27']")).click(); //selecting return jouney driver.findElement(By.id("calendar1")).click(); driver.findElement(By.xpath("//td[text()='Oct']/../..//a[text()='3']")).click(); 
+3
source

You can directly send the date or time:

  WebElement dateBox = driver.findElement(By.xpath("//form//input[@name='bdaytime']"));  //Fill date as mm/dd/yyyy as 09/25/2013  dateBox.sendKeys("09252013");  //Press tab to shift focus to time field  dateBox.sendKeys(Keys.TAB);  //Fill time as 02:45 PM  dateBox.sendKeys("0245PM"); 
0
source

Below is a screenshot of the date selection field that we use on our portal, and below is the code that we use to select the highlighted date in the date selection list.

IMAGE CLICK ME

 if (!TestHelpers.IsElementPresent(By.Id(""), timeout, driver, verificationErrors)) return false; if (driver.FindElement(By.Id("")).Enabled) { if (driver.FindElement(By.Id("")).Text == "") { driver.FindElement(By.Id("")).Click(); if (!TestHelpers.IsElementPresent(By.CssSelector("table.ui-datepicker-calendar > tbody > tr > td.ui-datepicker-days-cell-over.ui-datepicker-today > a.ui-state-default.ui-state-highlight"), timeout, driver, verificationErrors)) return false; driver.FindElement(By.CssSelector("table.ui-datepicker-calendar > tbody > tr > td.ui-datepicker-days-cell-over.ui-datepicker-today > a.ui-state-default.ui-state-highlight")).Click(); if (driver.FindElement(By.Id("")).TagName == string.Empty) return false; } } 
0
source

There are multiple ways to do this in Selenium. Some web pages provide the option to select Years and Months, as we have on Windows. On some web pages, such as REDBUS.in, there are only Next and Previous buttons to select the year and months. I am writing a reliable but general method that will apply to all types of calendar events.

  • Get XPath Calendar.
  • Scroll to the desired Year and Month by comparing the current WebElement with the expected value. You can use the text values โ€‹โ€‹of the year and month after you reach the Calendar item.
  • Take all the td items in the list for this year and month.
  • Scroll through the item and click when your expected date matches the item in the list.

You may need to apply your logic to specific requirements, but this logic to get dates will always work.

0
source

Source: https://habr.com/ru/post/1244410/


All Articles