Having difficulty in choosing an option from a constantly changing drop-down menu

Having trouble clicking and selecting a value from a dynamic drop-down menu? Take a look at the code snippet below:

public static void main(String[] args)
{
    // TODO Auto-generated method stub
    //System.setProperty("webdriver.chrome.driver", "C:\\Chrome Driver\\chromedriver.exe");
    //WebDriver driver = new ChromeDriver();

    WebDriver driver = new FirefoxDriver();
    driver.get("http://www.spicejet.com/");

    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS)

    driver.findElement(By.xpath(".//*[@id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
 }

I've also noticed that Eclipse continues running after opening Spicejet.com without clicking on any dropdown options. To stop the execution, I have to manually click on the Terminate button or it will continue running for several hours (4-6 hrs, approximately).

Answer №1

If you're looking to choose a specific value, check out this code snippet where I've selected Goa (GOI). Just a heads up, it's not your typical dropdown menu - it's actually a web table.

WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.spicejet.com/");

    driver.findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXTaction")).click();
    driver.findElement(By.xpath("//div[@id='dropdownGroup1']/div/ul[2]/li[4]/a")).click();

Answer №2

Whenever I access that particular site, there are times when it appears to be waiting for the user to indicate whether they want the mobile version or desktop version. Could this be causing the delay?

If I choose to proceed beyond that prompt (using either Selenium code or manually):

 x1path = "//a[@class='desktop-view-button']"
 WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
 driver.find_element_by_xpath(x1path).click()

the above code triggers the dropdown menu:

 x1path = '//*[@id="ctl00_mainContent_ddl_originStation1_CTXTaction"]'
 WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
 driver.find_element_by_xpath(x1path).click()

However, after that step, you still need to select which specific element from the dropdown menu you want (which I believe your code does not do).

 x1path = '//div[@id="dropdownGroup1"]/div/ul/li[6]/a'
 WebDriverWait(driver,15).until(EC.presence_of_element_located((By.XPATH,x1path)))
 driver.find_element_by_xpath(x1path).click()

The ul/li[6] part of the code is responsible for selecting the 6th element in the first column (Belagavi).

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

Add a random number to the end of your first name

Here is the HTML I am working with: <input name="txtVFNAME" tabIndex="2" id="txtVFNAME" style="width: 150px;" onkeydown="CallRestricted("vfname")" type="text" maxLength="35"/> I am trying to add an increasing digit at the end of the Name every time ...

Choosing a Selenium web element can sometimes be tricky, especially when the class of the element changes depending on

When the mouse hovers over dropdown values, the class changes (Active value)https://i.stack.imgur.com/qz3ah.png I managed to make it work for 'Individual' driver.findElement(By.cssSelector("div[class=ui-menu-item-wrapper]")).click(); Even thou ...

Press the "Load More" button on Jooble using Selenium in Python

I am currently attempting to perform web scraping on this website and seeking a method to activate the load more button using selenium in Python. I have experimented with the following code snippets: driver.find_element(By.LINK_TEXT, "Load more") ...

Guide to Using Selenium sendKeys When Element Cannot Be Found on Page

Currently working on a selenium driver program that automatically logs in, but I'm struggling to locate the login form. Any tips on how to input text in the password and account fields on this form? The blank form requires text input ...

`Watir - Timeout:Issue - Ignoring Sleep Functionality`

Concern: Our website's loading time is sluggish after entering a user id and password. In an attempt to alleviate this issue, I implemented a "Sleep" statement along with a wait_until_present method. The page usually loads after about 70 seconds, yet ...

Selenium Chrome Driver can be used in headless mode to easily print a PDF document

Printing works fine without headless mode, but as soon as I switch to headless mode, it won't print a PDF. In my current project, which has a GUI, I prefer not to expose the Selenium webdriver to the end user. For this specific project, I'm usin ...

Optimal Python syntax for Selenium usage

Hello everyone, this is my first time posting here so please bear with me as I may not have all the correct terminology. I couldn't find any related information on my issue. I'm attempting to use find_element_by_css_selector, but I keep receivin ...

dealing with the StaleElementReferenceException while using selenium

Trying to create a web crawler using Selenium, I encountered a StaleElementReferenceException in my program. Initially, I suspected that the exception was caused by crawling pages recursively and navigating to the next page without first returning to the p ...

Best practices for making an AJAX call to fetch information from a database

I have a database containing a single table. The table includes columns for Company and Time, among others, with Company and Time being crucial. Users can make appointments by filling out a form. Within the form, there are 2 <select> elements - one ...

Guidance on selecting a checkbox with a dynamically generated identifier using WebDriver

I'm facing an issue with a form that contains a checkbox. The id of the checkbox changes every time a new form is opened, making it difficult to identify and interact with using Selenium-Webdriver in C#. Is there a way to locate and click on this dyn ...

Unable to interact with Selenium button element (type=button)

I am facing an issue with clicking a button on a specific website: The site in question is: Although the program successfully opens the website, it does not allow me to click on the "Load All" button to display all data. Upon inspecting the HTML code, I ...

Ways to access a global ArrayList in a separate method

I recently started using selenium and implemented Page Factory. Here is the issue I am facing: At the class level, I have declared a public arraylist (let's call it List1) which stores values from method1, and it works fine when printed within the sa ...

The Chrome Driver indicates compatibility with version 114, but it is actually intended to support version 113 - as mentioned in Selenium Python

from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.chrome.service import Service from webdriver_manager.chrome import ChromeDriverManager from time import sleep driver = webdriver.Chrome(service=Service(Chr ...

Tips for extracting the second item from a nested list in Python

I have created a Python Selenium script to extract data from a website and store it as a list. I now need to only retrieve the second element from the sublist that was generated. Please refer to the code snippet below which shows how the initial list is cr ...

Experience the power of Stimulsoft reporting integrated with Angular and Asp.net Core-framework!

I've created a Webservice in Asp.net Core to generate reports which is then called in an Angular app to display the reports. However, when I try to show the report in Angular, it only shows a blank page. What could be causing this issue and how can I ...

Determining If an Element is Present on a Web Page Using Selenium WebDriver and C#

Despite my extensive search across countless webpages, I have not been able to find a satisfactory answer to my question. Currently, I am working with Selenium 2.30 using C#. I have attempted the following approaches: if (browser.FindElement(By.XPath("xp ...

Learn how to utilize the foreach loop in C# to iterate through an array and retrieve its

{ "Config": { "controls": [ { "id": 1, "name": "test", "inputs": [ { } ] }, { "id": 2, "name": "xyz", "inputs": [ { } ] } ...

What is the method to choose an invisible link without an identifier using Selenium WebDriver?

I am attempting to click on the link labeled 'User Requested'. This link only appears when hovering over the menu. This is the HTML code: <ul class="k-widget k-reset k-header k-menu k-menu-horizontal" id="menu" data-role="menu" tabindex="0" ...

What is the best way to avoid using double quotes within a Xpath expression in Selenium?

An illustration: By.xpath("//*[@id="ext-gen1035"]/div/div[3]/i") ...

"Selenium WebDriver is capable of interacting with ExtJS combo boxes that share the same XPath, IDs, and CSS paths but have dynamically changing IDs. With its functionality, it allows

I'm facing a challenge with testing a web application built on extJS that features multiple drop-down combo boxes. Each box shares the same class name, and their IDs change every time the page reloads. These combo boxes are not your typical drop-down ...