A pair of identical dynamic drop-down menus selected using Selenium WebDriver technology

I am facing an issue with two dynamic drop down lists that are quite similar; I am encountering difficulty in selecting options from the second drop down list. Provided below is the HTML code snippet:

<div id="list" class="x-list">
<div id="list-list" class="x-list-list-ct">
<ul>
<li class="x-item" role="option">US Dollar - USD</li> 
<li class="x-item x-list-selected x-list-item-over" role="option">Afghan Afghani - AFN</li> //<-- this represents my initial selection from the first dropdown list
<li class="x-item" role="option">Albanian Lek - ALL</li>
<li class="x-item" role="option">Algerian Dinar - DZD</li>
<li class="x-item" role="option">Angolan Kwanza - AOA</li>
<li class="x-item" role="option">Argentine Peso - ARS</li>
</ul>
</div>
</div>

<div id="list" class="x-list">
<div id="list-list" class="x-list-list-ct">
<ul>
<li class="x-item x-list-selected x-list-item-over" role="option"> US Dollar - USD</li> //<-- this is the default hover selection
<li class="x-item" role="option"> Afghan Afghani - AFN</li> 
<li class="x-item" role="option"> Albanian Lek - ALL</li>
<li class="x-item" role="option"> Algerian Dinar - DZD</li>
<li class="x-item" role="option"> Angolan Kwanza - AOA</li>
<li class="x-item" role="option"> Argentine Peso - ARS</li>
</ul>
</div>
</d

Whenever an element is selected or hovered over, the class name changes to 'x-item x-list-selected x-list-item-over'

To select an item from the first list, I use the following command: driver.findElement(By.xpath("//li[text()[contains(.,'Afghan Afghani - AFN')]]")).click();

However, when attempting the same for the second list, although the element gets selected, the list does not close.

Answer №1

Here is a suggestion to try:

To select from the first list:

driver.findElement(By.xpath("//div[1]/div/ul/li[text()='Afghan Afghani - AFN']")).click();

To select from the second list:

driver.findElement(By.xpath("//div[2]/div/ul/li[text()='Afghan Afghani - AFN']")).click();

Answer №2

Consider the following XPaths to navigate through the elements on the page:

"//div[@id='list'][1]//li[text()='Afghan Afghani - AFN']"

or

"//li[text()='Afghan Afghani - AFN'][1]"

and
"//li[text()='Afghan Afghani - AFN'][2]"

It is likely that the list will close automatically when interacting with other elements on the page. For example, try sending a blank space to another element like a button to make the list lose focus and close by itself.

Answer №3

Consider using the following code snippet

(//div[@id='list'])[1]

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

Switching up the Facebook Marketplace location using Selenium

Regarding scraping Facebook marketplace from multiple locations, I am facing an issue with changing my location in the marketplace. Despite trying the code provided below, I have been unsuccessful in achieving the desired outcome. Can someone suggest an ...

The waiting time for ExpectedCondition.invisibility_of_element_located seems to be extended (using selenium web driver-python)

I have implemented the following ExpectedCondition method to verify that an element disappears before my test continues: wait.until(EC.invisibility_of_element_located((By.XPATH,busyIndicator))) My current process involves clicking on a save button, which ...

Having trouble interacting with element - Selenium WebDriver is not allowing the click

I'm attempting to select the Checkout Button. This is what it looks like : https://i.stack.imgur.com/TiMEO.png And here's the HTML snippet for it : <div id="buy-button-next"> <span data-reactroot=""> <div data-cid="buy- ...

Is there a way to simulate mouseOver in IE8 and Firefox using WebDriver?

Issues with the moveToElement function in WebDriver have been noticed specifically in Firefox and IE, while working smoothly in Chrome browser. Below is the code snippet that I am currently using: Actions builder = new Actions(driver); builder.mov ...

How to download a file through a hyperlink using PhantomJS with the help of Selenium

Using selenium to automate a click function on a hyperlink loaded on a specific page presents an issue. The script successfully executes in Google Chrome, but fails in PhantomJS. What could be causing this discrepancy? from selenium import webdriver driv ...

To successfully extract the transaction name, request name, iteration number, and user names utilizing the Selenium WebDriver sampler within JMeter, is a key priority

I am trying to retrieve the names of requests and transactions from my results. My goal is to display the transaction name, request name, iteration count, and the user number passed in my code using the Selenium Webdriver Sampler. Can you please share some ...

Install an unpacked extension for Selenium in node.js from a file

I'm inquiring about the process of adding an unpacked extension from my computer to my Chrome WebDriver. I found some information on how to do it here: https://seleniumhq.github.io/selenium/docs/api/javascript/module/selenium-webdriver/chrome_exports ...

Step-by-step guide on using Selenium and Java to retrieve and store comments

I am working on a Java application that utilizes Selenium Web Driver to extract data from Google Play Store apps. I currently have 30 links to different applications, and I am facing an issue with gathering ALL comments from each app. For instance, when ...

When Button is clicked, it freezes indefinitely without a timeout

I'm currently learning Selenium and facing an issue with a button click function on a web page. Sometimes, the button.click() command freezes without raising any exceptions and fails to continue executing. Is there a way to prevent this freeze? Perha ...

Retrieve the browser version through the use of the Selenium WebDriver

Is there a way to retrieve the browser version? >>> from selenium import webdriver >>> driver = webdriver.Chrome() >>> print(version) <-- Any suggestions on how to accomplish this? Chrome 92.0 ...

What's the best way to gather information from websites powered by a template engine?

Currently, I am attempting to extract data from a website using the tools scrapy and selenium. Initially, when I viewed the output of [ {{ certificant.FirstName }} {{ certificant.LastName }} ] I suspected that the issue may be due to the page still loadin ...

Is it possible to easily extract the text linked to a label that exists in a radio button using Selenium WebDriver?

Looking at the html snippet below, how can I determine if "Male" is already selected? <input id="radio1" type="radio" value="1" name="sex> <label class="radio" for="radio1">Female</label> <input id="radio2" type="radio" value="2" name ...

A method for determining the number of elements (size) in a List of WebElements

Currently, I am attempting to validate the number of elements (specifically links) in the sub menu of the website by using an Assert statement declaring that there are more than 3 elements present before actually clicking any of the links. However, the A ...

WebDriverException: Unable to establish the new session. Configuration not found for {platform=WINDOWS, browserName=FIREFOX, version=3.6}

Recently, I have started learning about Selenium Web driver and Grid 2. While attempting to execute a test case, I encountered an exception: Exception in thread "main" org.openqa.selenium.WebDriverException: Error forwarding the new session cannot find ...

Display all active ajax/http requests using Javascript or JQuery

Currently, I am constructing a test suite with Selenium. In case of a failed test, I require the ability to determine which vanilla http requests and ajax requests are still in progress. Is there a method to interrogate the browser directly and retrieve a ...

Chromedriver 115 flagged as malicious trojan

Struggling with chromedriver 115 as my company's firewall flags it as a trojan. Even manual downloading doesn't work. Has anyone faced this before? Any solutions that don't compromise the company's security? Thank you. I requested our ...

Selenium is unable to function properly with a chromedriver that has been altered to evade detection

My question arises from a specific issue I encountered while using Selenium for web scraping. As seen in this thread and this thread, the suggested solution to modify the ChromeDriver no longer seems to work effectively. Despite the advice provided in an o ...

StaleElementReferenceException: the element no longer exists in the page document and is considered stale

Encountering an issue with my selenium java project where I keep receiving an error message. for(WebElement link:AllTheLinkList) { if (link.getAttribute("href") != null && ! link.getAttribute("href").contentEquals("javascript")) { ...

Error page reached: The server located at x is experiencing delays in its response time

I am looking to deploy my web scraping application on Heroku. This application is designed to scrape data from various apartment websites using multiple selectors for each URL. The application runs on APScheduler but I am encountering an error in the logs: ...

Encountering a local error in setting an [ ] in a Selenium web scraping tool

Recently, I came across a piece of Python code that has left me puzzled as I do not have much background in this language. The issue at hand involves an unbound local error and it seems like a trivial mistake on my part. UnboundLocalError ...