Failing to verify the presence of specific text within a dropdown menu using Selenium

Previously, I successfully implemented this code, however, the HTML/CSS for the dropdown has since changed and now I am unable to get it to function correctly.

Below is the structure for the dropdown code, with specific text highlighted that I am trying to identify:

I have set up a web element for the button that triggers the dropdown, retrieved its text, and checked if it contains certain criteria:

val myDropDown = driver.findElement(By.xpath("""//*[@id="no-jcf"]/div[2]/div[3]/form/div[18]/div[2]/div[2]/ol/button"""))
val ans = myDropDown.getText().contains("Copy of Invoice")

Despite following these steps, the code is not functioning as expected and returning an incorrect result.

Answer №1

To verify a particular option, you can follow this method:

WebElement dropdownElement = driver.findElement(By.xpath("//button[contains(@class,'dropdown-toggle')]//li[@data-value='8']//span[1]"));
String dropdownText = dropdownElement.getText();
if(dropdownText.equalsIgnoreCase("Copy of Invoice"))
{
//...
}

Answer №2

There are a couple of key mistakes that I have identified:

1) The xpath provided points to the button element, but the dropdown menu is actually not a child of the button. It is the next sibling of the button. Therefore, attempting to extract text from the button will not retrieve the options within the dropdown menu.

2) When using the getText() method, it retrieves the visible inner text of an element and its subelements. In order for this method to work correctly, ensure that the dropdown menu options are visible either by clicking on them or hovering over with a mouse.

Answer №3

To streamline the process, create a collection of web elements and identify the specific one you are looking for. Here's an example:

List<WebElement> elementCollection =
driver.findElement(By.Id("unique-id").findElements(By.tagName("element"));

Now that you have gathered all the elements under the specified ID, loop through them using a for/foreach statement to determine if the desired element is present:

for(WebElement element : elementCollection )
{
   if(element.getText().contains("Desired Text"))
   {
   //... add your desired actions here
   }
}

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

Angular 2 - Karma is having trouble locating the external template or style file

The file structure for this project (taken from the Angular 2 official site) is as follows: https://i.stack.imgur.com/A6u58.png Upon starting Karma, I encountered errors indicating that two files were not found under /@angular/... To resolve this issue, ...

Encountering an attribute error when using ActionChains' move_to_element() method?

While running my python/selenium script, I encountered the error message: AttributeError: 'WebElement' object has no attribute 'move_to_element' This error seems to indicate that it does not recognize 'move_to_element' as a ...

What is the best method for enclosing all text within an HTML document with a different element?

For example: FROM <div> whats up! <div> whats up2! </div> thank you for your help <div></div> </div> TO <div> <span>whats up!</span> <div><span> whats up2! </span&g ...

Using jQuery to remove the last two characters from a specified class

I have a simple task at hand. I am trying to use the slice method in JavaScript to remove the last two characters from a string that is generated dynamically within a shopping cart. Instead of displaying a product as $28.00, I want it to display as $28. S ...

Determine the hierarchy of test methods in TestNG XML

Query: How can I assign method priority in TestNG XML without specifying it at the class.method level using a priority tag? I've searched for an answer to this question but have not been able to find any information on this specific issue. Scenario: ...

Leveraging periods within a MySQL database: Node.js for seamless updates

I am currently facing a challenge in updating a column name that contains a period in node using node-mysql. I appreciate the convenience of being able to update multiple columns by providing an object with keys, but the string escaping process with node-m ...

Javascript downloadable content available for download

<div class="center"data-role="content" id="content" > <a href="#flappy" data-transition="slide" >Avoid Becoming a Terrorist</a> </div> <div id="flappy" > <center> <div id="page"> ...

Generating a chart using solely the existing data components (values)

I have the following arrays: const elements = ['12345', '12346', '12347', '12348', '12349', '12350']; const fruits = ['Apple', 'Ball']; I want to create a Map using array ele ...

"Invalid: string path required" (version 5.10.0)

There's this file (a large bundle of a couple of JS files) that used to work perfectly with browserify (version 5.10.0) until just recently, but now it's not cooperating. Here's the command I'm using: $ browserify index.js -o dist/out ...

The Email field encountered an ElementNotInteractableException error while attempting to send text

Problem Statement Currently facing a challenge logging into this login page using Python and Selenium. Solution Attempt url = 'https://activeshop.com.pl/customer/account/login/' browser.get(url) # providing login credentials account_name = ...

Leveraging the power of Javascript/jQuery to manipulate form

On my website, I have implemented a form that requires a customized response for certain zip codes. To achieve this, I am developing a code that validates the first 3 digits of the entered zip code against a predefined array in my system. Although the code ...

Node.js is raising an error because it cannot locate the specified module, even though the path

Currently in the process of developing my own npm package, I decided to create a separate project for testing purposes. This package is being built in typescript and consists of a main file along with several additional module files. In the main file, I ha ...

Reset an Angular service's public variable

I recently started working with Angular and encountered an issue that I couldn't find a solution for on Google or Stack Overflow. I believe my problem lies in not knowing what to search for. Here is the code snippet causing trouble: JSFiddle HTML &l ...

Enter the ISO code for the country in an HTML form to submit

I'm currently working on a form that requires users to select a country from a list. I came across a website that provides a comprehensive list of countries in HTML format. However, the site displays the full country name but sends the ISO code to the ...

Creating personalized HTML logs

My automated test script is set up in a way that automatically generates an HTML log file upon completion. You can find instructions on how to do this here. Is there a possibility to customize this HTML log or create my own HTML logger to replace the defa ...

Exploring the art of path zooming in d3.js

Trying to resolve this issue, I adjusted the geoJsonURL to handle a more intricate shape. Despite the new shape allowing the zoom method to function correctly, the shape itself appears completely distorted. Based on the Coordinate system: WGS 84 (EPSG:4326 ...

Remove attributes from a collection of objects

Here is an array of objects: let array = [{ firstName: "John", lastName : "Doe", id:5566, weight: 70 },{ firstName: "Francis", lastName : "Max", id:5567, weight: 85 }]; I am looking to remove the properties "lastName" and "weight" for all obj ...

AngularJS validation for minimum character count prevents character overflow

Encountering an unusual "issue". I've set up a form with a textarea that has both a minlength and maxlength validation. In addition, there's a straightforward character count displayed: <textarea ng-trim="false" ng-model="form.text" minlengt ...

Dealing with JSON Stringify and parsing errors in AJAX

I've been troubleshooting this issue for hours, trying various suggestions found online, but I'm still encountering a problem. Whenever I encode function parameters using JSON.stringify and send them to my PHP handler through AJAX, I receive a pa ...

Using a pool.query with async/await in a for-of loop for PostgreSQL

While browsing another online forum thread, I came across a discussion on using async/await with loops. I attempted to implement something similar in my code but am facing difficulties in identifying the error. The array stored in the groups variable is f ...