Choosing the current date and time (+15 minutes) from a date picker in Selenium:

I am facing difficulty in selecting today's date and the current time plus 15 minutes from the date picker using Selenium.

The code I have written for the date selection is not working. Here is the code snippet:

 DateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy hh:mm a");
    System.out.println("Format defined");
    Date date2 = new Date();
    System.out.println("Date object creation");
    String today = dateFormat2.format(date2);
    WebElement dateWidget = driver.findElement(By.xpath(".//*[@class='xdsoft_calendar']"));
        System.out.println("Calendar web element");
        List<WebElement> columns=dateWidget.findElements(By.tagName("div"));
        System.out.println("listing web element");
        System.out.println(today);
        //comparing the text of cell with today's date and clicking it.
        for (WebElement cell : columns)
        {
            System.out.println("In for loop"+cell.getText());
            if (cell.getText().equals(today))
            {
                System.out.println("inside if");
                System.out.println(today);
                cell.click();
                break;
            }
        }

Date picker https://i.sstatic.net/9igbs.png

<div class="xdsoft_calendar">
   <table>
      <thead>
         <tr>
            <th>Sun</th>
            <th>Mon</th>
            <th>Tue</th>
            <th>Wed</th>
            <th>Thu</th>
            <th>Fri</th>
            <th>Sat</th>
         </tr>
      </thead>
      <tbody>
         <tr>
            <td data-date="28" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week0 xdsoft_date xdsoft_disabled xdsoft_other_month xdsoft_weekend">
               <div>28</div>
            </td>
            <td data-date="29" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week1 xdsoft_date xdsoft_disabled xdsoft_other_month">
               <div>29</div>
            </td>
            <td data-date="30" data-month="9" data-year="2018" class="xdsoft_date xdsoft_day_of_week2 xdsoft_date xdsoft_disabled xdsoft_other_month">
               <div>30</div>
            </td>
         </tr>
      </tbody>
   </table>
</div>

Note: 1. The expected outcome is to be able to select today's date and the current time plus 15 minutes from the date picker. 2. I am finding this task very challenging, any assistance would be greatly appreciated.

Answer №1

The question is missing some key details, specifically regarding the appearance of the html page for time selection. However, I will do my best to provide assistance based on the information available.

If you are unable to select the current date, it is likely due to the condition not being met:

if (cell.getText().equals(today))

The variable 'cell' is a div and its getText() should return values like 28, 29, and 30. You are comparing this with dateFormat2.format(date2); which likely returns something like 08/11/2018 02:12 PM. Here is a suggested modification using Java 8+:

int currentDay = LocalDate.now().getDayOfMonth();
if (cell.getText().equals(currentDay)) {

}

For handling hours and minutes, either provide information on the html structure or use LocalDate.now().getHour() to get the current hour and create a similar condition for selecting the desired time. You may need to scroll to the specific element. A helpful resource for this is LocalTime here

Answer №2

  1. Get the current time : Calendar cal= Calendar.getInstance();

  2. Add 15 minutes to the current time : cal.add(Calendar.MINUTE, 15);

  3. Convert the updated time to a Date object : Date date = cal.getTime();

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best method of transferring all procedures from frida javascript rpc.exports to python?

I have a JavaScript file containing rpc.exports rpc.exports = { callfunctionsecret: callSecretFun, callfunctionsomethingelse: callSomethingElse, } I am trying to retrieve a list of all these functions in Python, but I have been unable to find a so ...

Correct validation is not achieved when the "!" symbol is used in the matches function

I am working on a React and Next.js project where I am using Formik for handling forms and Yup for validations. One specific input field requires some validations to be performed. This field must be required, so if the user does not enter any information, ...

Launch a new window for a div when a button is clicked

Hey everyone, I need some help with opening a div in a new window. Currently, I am using window.open and it is working fine. Here is the code snippet: <div id="pass">pass this to the new window</div> <a href="#" onclick="openWin()">clic ...

Extracting news headlines from a website with continuous scrolling

I'm trying to extract headlines from a specific website: In order to retrieve older news, clicking on the blue button labeled "SEE MORE" is required. Although I wrote this code snippet, it didn't yield the desired results: from bs4 import Beaut ...

Iterating through the keys of JSON objects in an Android app

I recently encountered an issue where I had to convert XML data to JSON programmatically. Here is a snippet of the XML structure: <channel> <title></title> <link>index.html</link> <description>...</description> &l ...

Determining with jQuery if the right element has been successfully "dropped"

Imagine having 10 different draggable boxes and 2 droppable areas. You need to correctly match 5 boxes with droppable area 1 and the other 5 with droppable area 2. If a box intended for droppable area 1 is mistakenly dropped into area 2, it should not wor ...

JavaScript AJAX request from client side to a CORS-enabled server does not pass along user credentials

I've integrated Shiro into my web application to handle security for the REST API. The setup works perfectly when the client side is hosted on the same server, but I'm facing issues with CORS requests. After some investigation, here's what t ...

Exploring the world of Python and Selenium with questions about chromedriver

I am planning to share my Python program with others and I would like to convert it into a .exe file using py2exe. However, I encountered an issue while using Selenium. chrome_path = r"C:\Users\Viktor\Desktop\chromedriver.exe" If user ...

The Spring Boot API serves up raw JSON data without any naming labels

In the process of developing a REST API using Java Spring Boot, I have encountered an issue related to the format of the result. Here is the setup: @RestController @RequestMapping("/api/readings") public class Readings { @Autowired private Reading ...

Is it possible to loop through an array that is defined within a for loop in Java?

Is there a more elegant way in Java to iterate through an array of Strings without having to declare the array separately? for (String s : {"HEY", "THERE"}) { // ... } The only alternative I could think of is: for (String s : new ArrayList<String ...

Creating applications with Angular2 and TypeScript is possible even without utilizing NPM for development

I've seen all the guides recommend installing npm, but I'm determined to find an alternative method. I found Angular2 files available here, however, none of them are in TypeScript code. What is the best course of action? Do I need Angular2.ts, ...

Insert the ng-if directive into an element using a directive

I am working on an AngularJS directive that involves looking up col-width, hide-state, and order properties for a flexbox element based on its ID. I want to dynamically add an ng-if=false attribute to the element if its hide-state is true. Is there a way ...

Filter an array containing objects within objects and use the reduce method to calculate the total value

Here is an array of objects that I'm working with: const data = [ { "order_id":38795, "order_type":"Music", "date":"2021-08-14", "name":"Concert ...

Click on the div to automatically insert its text into the textarea

I am looking for a way to enable users to edit their posts easily. My idea is to have them click on a link, which will then hide the original div containing their post and reveal a new div with the old text inside a textarea for editing. Despite searching ...

Tips for adding React components to an array with the help of backticks

Currently, I am attempting to populate an array with icons by extracting the name from data and concatenating "<" and "/>" around it in order to convert it into an Mui Icon. Despite renaming the imported icons to match the names in the data, when I ...

I'm puzzled as to why my Vuex modules are not functioning properly. I keep receiving the error message: "[vuex]

I've been searching for hours and can't figure out why I keep getting this error message: [vuex] unknown mutation type: groceryStore/getStoreApple on all of the commits to the mutations in the groceryStore-module. I believe I'm following the ...

Troubleshooting node modules for browser compatibility

Looking for assistance with running a specific node module in a browser. The module in question is called fury.js. I attempted to use browserify, however, encountered an error stating "ReferenceError: fury is not defined" when trying to utilize it. In th ...

Sharing a Twitter thread using Twit library with Node.js

I have been using Node.js along with the npm Twit module to post tweets on Twitter, and while it works for a single tweet, I am facing issues when trying to post multiple tweets as a thread. When attempting to post a series of tweets together, they do not ...

Tips for utilizing jquery.load for fetching a section of an html document or an entire html file

I'm experimenting with jquery's load function to dynamically fetch and display HTML content, rather than using $.ajax(). I enjoy exploring the various features that jQuery offers and want to understand how each one works. Below is the code I am ...

Tips for accessing payment details from a stripe paymentElement component in a React application

Here is a basic code snippet for setting up recurring payments in Stripe: await stripe ?.confirmSetup({ elements, confirmParams: { return_url: url, }, }) After browsing through the documentation and the internet, I have two unanswere ...