Is it possible to locate an element using two attributes in C# WebDriver?

Can anyone assist me in finding elements based on their class and the text they contain? I have attempted the following methods without success, so any guidance would be greatly appreciated (I prefer using CSS for selection, but XPath is also acceptable):

Driver.FindElement(By.XPath("//td[contains(@class,'TestClass') and .//text()='TestText']"))
Driver.FindElement(By.CssSelector("td.TestClass:contains('TestText')"))

Sample elements to locate:

<td class="TestClass"> TestText</td>
  <td class="TestClass"> TestText1</td>
  <td class="TestClass"> TestText2</td>...

Answer №1

  1. To locate specific content, consider utilizing the XPATH expression provided below:

    //td[@class='TargetClass'][text()='DesiredText']
    
  2. Opt for FindElements over FindElement. This way, you can retrieve all td elements and iterate through them to identify the required text.

Answer №2

Here is the XPath you can use:

//td[text()=' YourTest']

Alternatively, you can also try:

//td[@class='YourClass' and text()=' YourTest']

I trust this information will be beneficial to you :)

Answer №3

Here's an alternative method that utilizes a CSS Selector and LINQ within C#:

    var items = WebDriver.FindElements(By.CssSelector("td.UniqueClass'"]));
    var selectedElement = items.FirstOrDefault( item => item.Text.Contains("SpecificText"));

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

Prevent clicks from passing through the transparent header-div onto bootstrap buttons

I have a webpage built with AngularJS and Bootstrap. It's currently in beta and available online in (German and): teacher.scool.cool simply click on "test anmelden" navigate to the next page using the menu This webpage features a fixed transparent ...

The functionality of sending emails via Hotmail SMTP C# is successful when tested locally, but encounters issues when deployed on a live server

I have encountered an issue with my asp.net web API that is designed to send emails to a Gmail account. The API works perfectly when both the backend and frontend projects are running locally, but it fails when deployed on live hosting (SmarterAsp). Here& ...

Steps for choosing a value from the autofill search bar in Selenium

I am looking to perform the following steps: Go to the website Type "computer" in the search bar View the suggested options in the auto-suggest feature Choose "computer keyboard" from the list Click on the Find button ...

Activate a CSS button upon clicking and update the content within the div

In order to achieve the desired result of having menu CSS buttons change the content of a div when clicked, I have managed to do that successfully. However, a problem arises when clicking on the 2nd or 3rd button/link - it changes the div content, but as s ...

How about: "Insert a line break inside a div element"

My goal is to have 3 icons positioned side by side that will float left if the window shrinks. Under each icon, I plan to include some text. Below you can see that I've made progress on this layout. .icons { BORDER-TOP: black 1px solid; HEIGHT: 10 ...

Tips for displaying the Bootstrap Navbar in a single line on the xs layout

I am working on a Bootstrap navbar that displays properly in most screen layouts, but I am having trouble getting it to show on one line in the xs layout. Even though there is enough room for it to fit on one line, it keeps breaking up into multiple lines. ...

I recently updated NHibernate and now I'm encountering the LoaderExceptions error

My website was functioning properly locally, but after transferring it to HostGator, I encountered an error on . The HostGator support team informed me that my application is running at a full trust level, while they only allow medium trust level. Upon fu ...

What is the best way to eliminate HTML unicode content from the final item in a continuously changing list?

li:after{ content:"\00b6"; } <ol> <li>banana</li> <li>orange</li> <li class="last-class">apple</li> </ol> I am currently working on a dynamic list where the number of <li> tags varies over t ...

AngularJS enables tab highlighting by responding to button selections

In my application, there are 3 tabs set up as follows: <li ng-class="{ active: isActive('#one')}"><a data-toggle="tab" class="tab-txt-color" href="#one" ng-click="selectTab('fruits') ...

Inject a snippet of temporary code at the end of the CSS file using JavaScript or JQuery

I am looking to dynamically add temporary CSS code to the end of an external CSS file or modify it using JavaScript or jQuery. For example, let's say my mystyle.css file looks like this: //mystyle.css p{color:red} Now, when a user interacts with the ...

Navigating through the sticky top navbar in Bootstrap 4 by swiping

I've encountered an issue with a sticky-top navbar that has dropdowns with multiple buttons each. On smaller devices, the navbar gets cropped and I'm unable to access the lower buttons. I'm looking for a solution to add scrolling to this nav ...

Guide to playing a gif solely through an onclick event using JavaScript

Below you will find a code that is used to load a gif animation from an array of characters, and then display it within a board class using the image src. I am looking to make the gif animation play only when the displayed gif is clicked. Currently, the ...

Tips for adjusting the appearance of a datagridview row when hovering over it:

How can I make the color change in a datagridview row when the user hovers over it? Currently, I am only able to get the gray rows to turn blue on hover, but I want this effect to apply to all rows except the footer as well. How can I achieve this without ...

Creating a dynamic and adaptive horizontal SVG line for your website

Understanding how SVGs function is still a bit unclear to me. I know that the viewBox property plays a role in scaling SVGs, and I have come across advice suggesting not to specify height and width attributes when creating responsive SVGs. While there are ...

Modifying the Title of a Google Calendar Event with the Google Calendar API v3 and Data Looping Functionality

I have successfully implemented retrieving events from my Google calendar using an API key. Displayed on my webpage are the events in the following format: 5/31/2016 9:00:00 AM - 5/31/2016 12:00:00 PM Event Title: Staff Meeting Description: Wee ...

Accessing Selected Item in Check Box Column within Data Grid View - C# Windows Application

I am looking to automatically update the Status column based on the Attendance_Status value. If the Attendance_Status is "Attended", I want to load the Checked Value in the Status column. If it's "Unattended", I want to uncheck the box. Here's a ...

Edge and Internet Explorer fail to recognize the :focus CSS selector

I am utilizing CSS to make our placeholders jump up when clicked inside, but Edge is not recognizing the CSS. It works fine on every other browser except for Edge. I've tried various solutions but none seem to work on Edge. Any thoughts on what might ...

While operating the Hybrid Framework in Selenium using Core Java, only local connections are permitted and access is restricted

As someone who is new to coding in a Hybrid Framework, I have been working on a simple application involving navigation and reporting the results in an HTML page through coding. However, I have encountered the following error: ============================ ...

Guide on creating an empty ArrayList in JSON using Rest Assured

When serializing the JSON from a POJO class, I encountered an issue where passing null or empty values to the properties in the POJO class resulted in getting [""] in the array list instead of []. Expected: "Key": [] But Received: "Key":[""] This is how ...

Searching for objects in a List using LINQ in an ASP.NET C# application is like exploring a digital

I am faced with the task of searching for a specific ID within one of the child objects of an object that contains child objects. The structure of the object tree is as follows: Sites (id, name) ->Zones[] (id, name) -->Placements[] (id, ...