Assistance with CSS Selectors in Selenium WebDriver for Date Selection

Could someone assist me in selecting a date from the small window using CSSSelector or any other method? Below is an example of HTML code for reference.

<td class=" " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">1</a>
</td>
<td class=" " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">2</a>
</td>
<td class=" ui-datepicker-week-end " onclick="DP_jQuery_1468593787056.datepicker._selectDay('#dp1468593787059',11,2016, this);return false;">
<a class="ui-state-default" href="#">3</a>

If you have any questions, feel free to ask. Thank you.

Answer №1

When using the JQueryUI DatePicker, this method should effectively select the desired date.

   public void chooseDate(int day, int month, int year) {
        int currentYear = Integer.valueOf(driver.findElement(By.xpath("//td[@data-year]")).getAttribute("data-year"));
        int currentMonth = Integer.valueOf(driver.findElement(By.xpath("//td[@data-month]")).getAttribute("data-month"));

        Boolean isNext = (year == currentYear && month >= currentMonth)
                || year > currentYear;

        String navTo = isNext ? "next" : "prev";

        String dayXpath = "//td[@data-year='" + year + "' and @data-month='" + month + "'][a[text()='" + day + "']]";

        Boolean found = false;
        do {
            List<WebElement> dayEl = driver.findElements(By.xpath(dayXpath));
            if (!dayEl.isEmpty()) {
                dayEl.get(0).click();
                found = true;
                break;
            }
        } while (navigateTo(navTo));

        if (!found) {
            System.out.println("Couldn't Find the specified date");
        }

    }

    public Boolean navigateTo(String nav) {
        List<WebElement> navigate = driver.findElements(By.xpath("//a[@data-handler='" + nav + "']"));
        if (!navigate.isEmpty()) {
            navigate.get(0).click();
            return true;
        } else {
            System.out.println("Unable to find link for navigation: " + nav);
        }
        return false;
    }

Example Usage:

    chooseDate(26, 6, 1992);

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

Optimizing CSS Styles for Mobile Devices

I am having trouble adjusting the CSS on my website to make it more mobile-friendly. The site renders correctly in major desktop browsers like Chrome, IE, and Firefox, but there are issues with the rendering when viewed on mobile Safari or Chrome. Interest ...

The spacing between images on mobile devices is excessive

My images on mobile have too much space between them: mobile version They appear fine in Dev Tools: dev tools version This is my HTML: <div class="container-fluid"> <!-- <br> --> <div class="row"> ...

An issue occurred during the building of the module (from ./node_modules/sass-loader/dist/cjs.js) and resulted in

Within the app directory, I've created a new folder called styles > custom > loader.scss. In the styles.scss file, I included the following import: @import "styles/custom/loader.scss"; However, upon doing so, an error message appeared ...

Align two divs with text in the center

I am having trouble centering two divs horizontally and aligning them between each other. The issue arises when I add text of different sizes in each div. Here is an example: http://jsfiddle.net/DgEcs/1/ Why does the red div move down and how can this be ...

Exploring z-indices in event bubbling

JSFiddle: https://jsfiddle.net/uLap7yeq/19/ Issue Let's examine a scenario where there are two elements, canvas and div, positioned in the same location using CSS. The div has a higher z-index compared to the canvas, but how can we make sure events ...

The combination of DOMDocument and DOMXPath is a powerful duo

I am currently working on converting CSS to inline using DOMDocument and DOMXPath. However, I'm encountering an issue where the HTML I provide is not being recognized as valid XML. Is there a way for me to configure these functions to ignore unknown ...

Are there any CSS selectors similar to :empty that can match elements with children that have display:none?

Looking for a solution to the issue stated in the title, this code snippet is causing some trouble: .group { width: 100px; height: 100px; background-color: blue; } .group:empty { background-color: red; } .hideme { display: none; } <div cl ...

Glowing semi-opaque about spotify?

Recently, I decided to challenge myself by recreating the Spotify homepage using only pure Javascript and SCSS as a way to test my front-end development skills. You can view my progress so far at this link, although please note that it's still a work ...

Discover a helpful tip for using Pentadactyl with StackExchange's voting buttons

Does anyone know how to enable hinting for voting arrows on StackExchange using Pentadactyl (a fork of Vimperator)? I've tried removing my .pentadactylrc file and restarting Firefox, but still can't figure out how to upvote questions and answers ...

Utilizing a ListView Below a LinearLayout: Step-by-Step Guide

In my Android project, I am working on creating the MyProfile activity. This activity will display a user's profile picture, name, and other bio data on the first screen. Additionally, I would like to make the layout swipeable so that when the screen ...

Bootstrap dropdown navigation bar menu

I recently delved into learning Bootstrap, and I came across an issue with the dropdown menu. My goal was to make it appear outside of my navbar, but for some reason, it seems stuck. Despite trying various methods, nothing seems to be working. Below is th ...

Creating dynamic forms and tables on an HTML webpage

I'm struggling to dynamically add table elements and form elements in an HTML page. My goal is to automatically add input boxes with labels when a user enters a number in the input box without having to click any button. I believe using the 'keyu ...

Creating a Bootstrap carousel using cards

My attempt to create a bootstrap carousel using cards on my website has hit a snag. I found some code here. The code snippet I used is as follows: <section class="carousel slide" data-ride="carousel"> <div class="container"> < ...

What could be causing the SyntaxError with JavascriptExecutor: Unexpected identifier?

Python PythonExecutor py = (PythonExecutor) driver; Boolean ready = (Boolean)py.executeScript("the following Python code"); Python Code var ready = False; window.onload = def check_ready(): ready = True def sleep(): return new Promise(resolve = ...

What is the role of the CSS URL property in web design?

Occasionally, I enjoy the challenge of "reverse engineering" websites, especially those with a complex design. By dissecting these sites, I aim to gain insight into the techniques employed by other developers. Recently, my curiosity led me to analyze the ...

Changing the container's height in an HTML document

enter image description hereim struggling to change the height of the white dash container, and I can't quite figure out how to do it. Here is the code, any help would be appreciated: analytics.html : {% extends 'frontend/base.html' %} {% l ...

Challenges in structuring an AngularJS Material Design project

When using Angularjs Material framework, I encountered an issue where aligning content (containing cards) to the center resulted in adjacent card heights increasing when one card's height was adjusted. See example code here. The goal is to have dynam ...

Using JavaScript to override a specifically declared CSS style

In the process of working with a database, I come across HTML that includes "spans" colored in different shades. An example would be: <div id="RelevantDiv">the industry's standard <span style="background-color: red">dummy text ever since ...

Enhancing Automation with Multiple Processes in Selenium Using Python (Cookie Clicker)

I'm looking to develop a cookie clicker bot that runs on one Chrome tab but in different processes to increase clicking speed import math import os from multiprocessing import Process, Pool, queues from selenium import webdriver from selenium.webdrive ...

Customizing the appearance of an HTML element using user-input text styling

On my webpage, there are two text areas and a division. One of the text areas is meant for users to input HTML code, while the other is for CSS code. Whenever a button is clicked, a JavaScript function will be triggered to display the combined HTML and CSS ...