Selecting an option containing text using Selenium and Xpath

During one of my tests, I am attempting to choose an option that includes the word "Current."

The dropdown menu appears as follows:

<select name="lead" class="wide">
<option value="">-- Select a lead --</option>
<option value="5066689">Current Lead: L5066689 (kitchens) Created: '27/08/2021 11:10:53'</option>
<option value="5066671"> L5066671 (kitchens) Created: '27/08/2021 09:56:10'</option></select>

I am trying to achieve this using the following function:

const chooseCurrentLeadToSaveAgainst = async () => {
    await pageHelpers.waitForSelectorAndClick(selectors.leadDropdown);
    await logger.logScreenshot();
    await pageHelpers.waitForSelectorAndClick(selectors.currentLead);
};

The leadDropdown selector is:

leadDropdown: 'select.wide',

This method works fine in clicking on the select box, but then I run into issues while trying to click on the option containing the text 'Current' using the currentLead selector:

 currentLead: 'option[(contains(text(),\'Current\')]',

Unfortunately, with this selector, the test execution fails to locate and click it. What might be incorrect about this selector?

Answer №1

Using backslashes in this way is not visually appealing. Instead, consider starting with // to target the current node as shown below:-

//option[(contains(text(),'Current')]

If you are certain that the value will remain constant, like 5066689, try using the value attribute.

Edit :

driver.click('select.wide').click('select.wide option:nth-child(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

When attempting to change a Component's name from a string to its Component type in Angular 9, an error is thrown stating that the passed-in type is

When working with Template HTML: <ng-container *ngComponentOutlet="getComponent(item.component); injector: dynamicComponentInjector"> </ng-container> In the .ts file (THIS WORKS) getComponent(component){ return component; //compo ...

Submit a form to the same page without reloading and toggle the visibility of a div after submission

Is there a way to submit a form without refreshing the page and hiding the current div while showing a hidden one? I attempted to use the following code, but it ends up submitting the form and reloading the page. <form method="post" name="searchfrm" a ...

Verify the inheritance of prototype, not the individual instance

Assume I have: function Pet(){} Pet.prototype.breathe = function(){} And function Cat(){} Afterwards, I execute: Cat.prototype = Object.create(Pet.prototype) Cat.prototype.purr = function(){} I am aware that var cat = new Cat() console.log(cat inst ...

Omit certain components from the JQuery ListNav plugin

I recently incorporated the Jquery plugin for record filtration. I obtained this plugin from the following link: After successfully implementing the plugin, I encountered an issue where it was counting the headings along with the alphabet filters in my co ...

How can you set a predetermined value for a dropdown menu after populating it with data from a Django database?

I currently have a query in my Views.py file that is used to populate a dropdown menu. The query works properly, but I need it to display a specific value that is stored in a variable based on the user's selection. For example, let's suppose we ...

The CanJS model is unable to retrieve data from a .json file

Implementing MVC using AMD in canjs with requirejs has been my current focus. Here's a look at my domains.json file: [ "1":{"uid": "1","urls": "domain1.abc.com"}, "2":{"uid": "2","urls": "domain2.abc.com"}, "3":{"uid": "3","urls ...

Struggling to securely post data to an Express server by hashing passwords with bcrypt?

I'm currently working on developing an API using Express and Sequelize. Specifically, I am writing a function to create a new user where I utilize bcrypt for password hashing. const createNewUser = (data) => { return new Promise(async (resolve, ...

The visualization rendered by ChartJS is not displaying correctly when data is fetched using an AJAX call

When it comes to creating visualizations from static datasets using ChartJS, I have no problems. However, the issue arises when I try to use data fetched via an AJAX call - the visualization does not render properly. Surprisingly, no errors are thrown even ...

How can I align Javascript output vertically in the middle?

I am currently facing an issue with a JavaScript clock displaying in a narrow frame: <!DOCTYPE html> <HTML> <HEAD> <TITLE>Untitled</TITLE> <SCRIPT> function checkTime(i) { if (i < 10) { ...

How can we identify if the user is utilizing a text-input control?

Incorporating keyboard shortcuts into my HTML + GWT application is a goal of mine, but I am hesitant about triggering actions when users are typing in a text area or utilizing the keyboard for select menu operations. I am curious if there exists a method ...

Difficulty locating the element

Recently, I delved into the world of coding and decided to create a form filler for Nike.com using the Selenium Chrome webdriver. However, I encountered a stubborn pop-up related to cookies that is preventing me from successfully filling out the form. You ...

Automating mobile websites using Selenium: Patience is key when waiting for hidden elements

I've encountered an issue with hidden elements on a fully loaded site. All items are available, and there is no need to load or switch pages. I have tried various options using ExpectedConditions, but it seems the elements are still not being waited f ...

Combine the array elements by date in Angular, ensuring no duplicates are present

How can array data be merged based on the date while avoiding duplicates? See the code snippet below: [ { date: [ '2019-12-02 08:00:00', '2019-12-03 08:00:00' ], upload:["47.93", "47.46", "47.40", "47.29" ], download: ["43.90", ...

Information on the Manufacturer of Devices Using React Native

Struggling to locate the device manufacturer information. Using the react-native-device-info library produces the following output. There seems to be an issue with handling promises. I need to store the device manufacturer value in a variable. const g ...

Struggling with ensuring that Angular-JS pauses and waits for an asynchronous call to complete before moving on

Understanding the use of promises in Angular for handling async operations is crucial, but I'm struggling to implement it effectively in this scenario. function fetchLineGraphData(promises){ var dataPoints = []; for (var i = 0; i < promise ...

Having trouble with the unresponsive sticky top-bar feature in Zurb Foundation 5?

According to the information provided on this website, it is recommended to enclose the top-bar navigation within a div element that has both the class "contain-to-grid" and "sticky". However, I have noticed that while the "contain-to-grid" class exists in ...

Transforming C# code into Python programming language

Hello, I have created a web scraper using Selenium in C#, but now I need to convert it into Python for my project. I am having trouble displaying the element in the console window. Here is the C# code: var elements = ghostDriver.FindElements(By.XPath("/h ...

chart for visualizing two rows with matching IDs and dates

I have been attempting to accomplish the following two scenarios: 1) When both ID and dates are the same in the chart, but the descriptions differ, I want to display them on separate lines. 2) If there is not enough room to show the row label, I would li ...

Tips for successfully transferring data using a dynamic key in a Semantic UI Vue dropdown

My current challenge involves troubleshooting the semantic-ui-vue dropdown functionality. To view the issue, visit my sandbox link: https://codesandbox.io/s/3qknm52pm5. Within the sandbox environment, there are two dropdown menus labeled as From and To. ...

Guide to correctly passing custom parameters along with the event object to an asynchronous form submission handler

Asking for guidance on defining and typing custom parameters alongside the native event object in an async onSubmitHandler for a form. The current implementation only receives the native event as a single parameter: const onSubmitHandler: FormEventHa ...