Unable to automate the selection of a dropdown menu using Selenium WebDriver

I am currently utilizing http://www.makemytrip.com/

This is the HTML code.

<div class="mrgnBot30 clearFix">
<span class="watch_icn flL"></span>
<div class="widget_inner clearFix suggest_me padBot15 flL">
<h3 class="clearFix hasBorderBottom">
<p class="clearFix checkDates">
<span class="check_date flL">
<label for="checkInDate">
Check-in Date:&nbsp;
<span id="checkInDate_day" class="dayLight">(Monday)</span>
</label>
<a id="checkInDateControl" class="cal_icn flL" href="#" tabindex="201"></a>
<input id="checkInDate" class="day flL hasDatepicker" type="text"  autocomplete="false"     value="01/21/2013"     name="searchCriteria.criterion.stayDateRanges[0].start" style="display: none;">
<span class="day date flL">
<select class="selectBox" tabindex="202" style="display: none;">
<span class="left_part flL"></span>
<span class="selectBox center_part flL selectBox-dropdown" style="display: inline-block; -moz-user-select: none;" title="" tabindex="202">
<span class="selectBox-label">21</span>
<span class="selectBox-arrow controls flR">
<a class="select_drop_icon flR" onclick="return false;" href="#"></a>
</span>
</span>
<span class="right_part flL"></span>
</span>
<span class="day month flL">
<select class="selectBox" tabindex="203" style="display: none;">
<span class="left_part flL"></span>
<span class="selectBox center_part flL selectBox-dropdown" style="display: inline-block; -moz-user-select: none;" title="" tabindex="203">
<span class="selectBox-label">Jan,13</span>
<span class="selectBox-arrow controls flR">
<a class="select_drop_icon flR" onclick="return false;" href="#"></a>
</span>
</span>
<span class="right_part flL"></span>
</span>
</span>
<span class="check_date last flL">
<span id="nights" class="nights flL">2 Night(s)</span>
</p>
</div>
</div>

There is a dropdown to choose a Check in date and Check out date. To be honest, I'm not sure how to programmatically select a value from the dropdown. During recording with Selenium IDE, it captured clicks on the dropdown values which adds to my confusion about its nature as a dropdown or not.

I attempted using a select statement to pick a value from the dropdown by identifying the Xpath value using firebug.

The code used:

 driver.get("http://www.makemytrip.com/");
 driver.findElement(By.xpath("//li[4]/a/span/span")).click();
    Select sel = new Select(driver.findElement(By
            .xpath("//div[2]/div/p/span/span/span[2]")));
    try {
        sel.selectByValue("21");
    } catch (Exception e) {
        e.printStackTrace();
    }

Unfortunately, this approach did not work for me. Below is the converted code snippet generated from Selenium IDE after recording.

Selenese Code:

driver.get("http://www.makemytrip.com/");
driver.findElement(By.xpath("//div[@id='chf_navigation']/ul/li[4]/a/span/span")).click();
assertEquals("I want to go to", driver.findElement(By.cssSelector("label")).getText());
assertEquals("Online Hotel Booking for Cheap, Budget & Luxury Hotels in India | MakeMyTrip.com", driver.getTitle());
driver.findElement(By.xpath("(//a[@onclick='return false;'])[2]")).click();
driver.findElement(By.xpath("(//a[@onclick='return false]")[3]")).click();
driver.findElement(By.xpath("(//a[@onclick='return false])"[4]")).click();
driver.findElement(By.cssSelector("li.selectBox-hover.selectBox-selected > a")).click();
driver.findElement(By.xpath("(//a[@onclick='return false]")"[5]")).click();

Here are the steps to locate and interact with the dropdown:

  1. Visit
  2. Click on the Hotels link
  3. You will find the Check in Date dropdown.

Please share any helpful suggestions to help resolve this issue. Thank you in advance.

Answer №1

Although you have shared the HTML code with us, we still require the actual code you have written for this program. Please provide us with your code and any error messages or exceptions that were displayed.

Answer №2

When selecting check-in dates, the IDs to use are b_checkin_day and b_checkin_month. Here is a guide on how to select them:

//select the check-in date <select> element
Element el = driver.findElement(By.id('b_checkin_day'));
Select sel = new Select(el)
// proceed with the selection

Check out dates should be selected using the IDs: b_checkout_day and b_checkout_month

Please note that this information is specific to the US version. The Indian version may have different HTML structures.

UPDATE : It appears you were referring to the Emirates website.

For selecting the day, here's the XPath to consider :

Element el = driver.findElement(By.xpath("//input[@id='checkInDate']/following-sibling::span[contains(@class,'date')]/select"))

The XPaths for month check-ins, day checkouts, and month checkouts respectively are as follows:

"//input[@id='checkInDate']/following-sibling::span[contains(@class,'month')]/select"
"//input[@id='checkOutDate']/following-sibling::span[contains(@class,'day')]/select"
"//input[@id='checkOutDate']/following-sibling::span[contains(@class,'month')]/select"

Your best solution is to utilize these XPaths for accurate selections

You can also simply verify the correct date in the form input fields by setting the desired date value in:

<input id="checkInDate" class="day flL hasDatepicker" type="text"  autocomplete="false"     value="01/21/2013"     name="searchCriteria.criterion.stayDateRanges[0].start" style="display: none;">

This method should work effectively (note the value="01/21/2013")

UPDATE2 :

Have you attempted to interact with the actual elements visible to the user? Start by clicking on the dropdown arrow (

"//input[@id='checkInDate']/following-sibling::span[contains(@class,'date')]/"
), which will display the select used by the JavaScript date selector. From there, you can make the appropriate selection by clicking on:
//ul[contains(@class,'selectBox-dropdown') and contains(@style,'block')]//a[@rel="25"]

If the desired day is 25, look at the <ul>s/<li>s at the bottom of the page to find the correct xpath for selection

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

Here is a unique rewrite: "Strategies for effectively passing the data variable in the geturldata function within Vue.js on the HTML side vary

How can I properly pass a variable in the getdataurl function in Vue.js? I need help with passing a variable in getdataurl function in Vue.js. Please provide a clear explanation and include as much detail as possible. I have tried doing some background r ...

automatically loading data using jquery

I am currently utilizing a windows service along with an html+j query page to interact with a web service. Whenever a Document is scanned on our device, I aim to display: name, country, and Passport number on our webpage. Although I have successfully a ...

Utilizing the controller specified in the template that has been included

Within this snippet of code, I am attempting to utilize a controller named FooCtrl that is defined in the included template app/foo.html, using the directive common.script. angular.module('common.script', []).directive('script', func ...

Counting the days: how to calculate without using negative numbers

After performing the calculation, the result shows -30. Is there a way to eliminate the negative sign and get 30 instead? $days = (strtotime(date("Y-m-d")) - strtotime($info['expiredate'])) / (60 * 60 * 24) echo abs($days); Any suggestions on ...

Facing challenges when running asynchronous mocha tests using async/await syntax

Working on E2E tests using mocha and selenium-webdriver has been quite a challenge for me. Although I have implemented async/await functions to handle most of the tests smoothly, I am facing an issue where none of the tests are completing successfully. Her ...

Identifying the camera model using getMediaStream

Are there methods available to determine if a user's device has a front, rear, or dual cameras installed? For instance, laptops typically only have a front-facing camera while some devices may have both. I am looking for a way to identify the type of ...

Having difficulties accessing the /playlists route with express and app.get in my code

I am encountering a 404 status code error when trying to access app.get('/playlists'). The browser displays "cannot GET /playlists" and I can't seem to figure out why. Here is the code I am using: var express = require('express&apos ...

What steps can I take to troubleshoot the "Element type is invalid" error in my React application?

I am currently restructuring my initial code for better organization. Click here to view the code on CodeSandbox. However, I'm facing issues with integrating child components into my code. For example, in this instance, I showcase how I import a chi ...

Implement the useEffect() function to handle the loading of external JavaScript on the client-side, replicating the

I have encountered a challenge while trying to integrate a rich text editor into my NextJS project. Since there are no available React components for this specific editor and it operates solely on the client side, I am required to load the necessary JavaSc ...

SPRING --- Tips for sending an array of objects to a controller in Java framework

Can someone help me with this issue? I am using AngularJS to submit data to my Spring controller: @RequestParam(value = "hashtag[]") hashtag[] o The above code works for array parameters but not for an array object. This is my JavaScript script: $http ...

How can I make Material UI's grid spacing function properly in React?

I've been utilizing Material UI's Grid for my layout design. While the columns and rows are functioning properly, I've encountered an issue with the spacing attribute not working as expected. To import Grid, I have used the following code: ...

"Learn how to dynamically update the value of a text box using a loop in jQuery when the

I am looking for a way to update the value of the second input field when the first one is changed. For example, if the user changes the value in "valuex00", I want the same value to be displayed in "valuey00". This should apply to all corresponding pairs ...

How can I fetch data from SQL using JavaScript based on a specific value in PHP?

My application is built using the Yii2 framework. Within my application, there is a view.php file that consists of an element and a button. The element, <div id="userId">, contains the user's login ID, and I aim to use the button to re ...

Detecting characters in jQuery's onKeyPress with case sensitivity

Is there a way to detect characters in a "case-sensitive" manner using the onkeypress event in jQuery? ...

What is the best way to retrieve a file's creation date using the file System module in Node.js

I'm having trouble fetching the filename and file creation date to send to a client. I tried using fs.stat which provides birthtime but does not include the filename. So, my question is: is birthtime equivalent to the file created date? How can I sen ...

Ways to retrieve the parent DIV's width and adjust the child DIV's width to match

Attached is my question with the HTML and a screenshot. Within a DIV container (DIV with ID=ctl00_m_g_a788a965_7ee3_414f_bff9_2a561f8ca37d_ctl00_pnlParentContainer), there are two inner DIVs - one for the left column TITLE (DIV ID=dvTitles) and the other f ...

Adjusting the amount of rows and columns in a fluid manner with CSS grid (updated)

After conducting my research, it seems that the most effective way to set the final value of a CSS grid is either by directly specifying it or by creating/manipulating the css :root value. A similar query was previously raised here, although the answers p ...

What is the simplest method to create a scroller for engaging narratives?

I am in the process of creating a static but responsive storytelling website using HTML. My objective is to create an effect similar to this: https://i.stack.imgur.com/zIEpU.gif The idea is to have text on the left and a *.jpg image fixed on the right. As ...

I attempted to create a test scenario to verify that the length of the tasks array is not negative. However, when trying to test this using 'not.toBe' in the code below, an error was encountered

app.js var todos=[]; todos=['to-do one', 'to-do two']; module.exports=todos; app.test.js const todos=require('./app') test('should have a length of at least 0',()=>{ expect(todos.length).toBeGreaterThanOrEqu ...

The issue with jspdf is that it is failing to generate PDF documents of

I'm currently developing a resume builder app using ReactJS. One of the functionalities I'm working on is enabling users to download their resumes as PDFs. However, I've encountered an issue with the generated PDFs when using jsPDF. The down ...