Choosing a Span Class Tag

Currently employing Selenium with C#

I am working on an automated test scenario that involves a user logging in as 'jonpark2'. Once the user successfully logs in, their username is stored within a span element's text. My task is to locate this element and perform an assertion check on it.

In my attempt to achieve this, I have the following code:

public static void Userloggedin()
{
    var userlogedin = Driver.Instance.FindElement(By.ClassName("username"));
    var selectelement = new SelectElement(userlogedin);
    selectelement.SelectByText("JonPark2");
}

Upon executing this code, I encounter the error message:

OpenQa.Selenium.Support.UI.unexpectedTagNameException: Element should have been select but was div.

The provided HTML snippet offers further insight into the element I am trying to target:

<div class="content-wrapper"> 
  <div class="float-left">
    <div class="float-right"> 
      <section id="login">
        <p>
           <a title="Manage your account" class="username" href="Account/Manage">
             Text - Empty Text Node
               <span class="username">
                 Text - JonPark2

Would appreciate any guidance or suggestions on what might be causing this issue?

Answer №1

Attempting to choose an item outside of a <select> element.

Here is a suggested fix:

public static void CheckUserLoggedIn()
{
    return (
        "JonPark2" == Driver.Instance.FindElement(By.cssSelector("section#login span.username")).Text;
    )
}

Answer №2

To target the specific element using XPath, you can use the following code:

var usernameElement = Driver.Instance.FindElement(By.XPath("//span[contains(@class, 'username')]"));

After locating the element, you can extract the text as shown below:

var textFromUsername = usernameElement.Text

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

PHP struggling to retrieve information from an HTML form

My PHP page is having trouble retrieving values from the HTML form. It keeps sending empty strings to the database. Below are my HTML and PHP code snippets. I am new to PHP and struggling to identify the issue. Please assist in finding the error. HTML s ...

Mac users may experience lag when using Javascript parallax effects

I created a parallax page where the background image changes using JavaScript translateY(- ... px)' similar to what you see on the firewatch website. On Windows, the parallax effect works smoothly. However, on macOS it is smooth only in Safari. All ...

Input text with JavaScript in Selenium

How to dynamically insert a value into a specific HTML element using JavaScript HTML code snippet: <div class="CodeMirror-code" style=""> <div style="position: relative;"> <div style="position: absolute; l ...

What type of technology is typically utilized in creating functional platforms such as goDaddy, wix, and other web design websites?

I am currently working on a web development project that aims to allow users to edit webpages without needing any coding knowledge. Users with authorization will be able to modify not only the text but also various HTML tags on the page, similar to platfor ...

Integrate a button within the .cs file

I am attempting to read the contents of a text file that contains image names and display them upon page load. Suppose the content in the text file is as follows: Australia Picture101 Singapore Picture201 Below is the code I have tried, but it doe ...

Adjust the HTML Canvas to cover the entire screen

I am trying to adjust the HTML Canvas element so that its width matches the width of the browser window and its height matches the height of the browser window. Below is the code snippet I am using: HTML: <body> <canvas id="gameCanvas" ...

Starting the download of the canvas featuring a stylish border design

I'm facing an issue where the canvas border is not showing up when I download the canvas with a border. The border appears on the screen, but it doesn't get included in the downloaded canvas. let canvas=qrRef.current.querySelector("canvas&qu ...

Using JavaScript to toggle the visibility of an HTML div element

I'm looking to enhance the functionality of this show/hide HTML div using JavaScript. HTML <a href="#" class="clickme">Menu 1</a> <div class="box"> <span><span class="labelText"><span></span>Number 1</ ...

"Ajax has the ability to alter the vertical scroll position

I have a quick query for you. Can anyone assist me in understanding why the page scroll resets to the top after an Ajax request? I suspect it has something to do with JQuery. There isn't much information available online, so I'm reaching out for ...

How can specific tags be removed from an XHTML document while preserving their content?

I am in search of a quick and efficient method to remove tags from an XHTML document. I believe there must be a simple solution among options like XSLT, XPath, XQuery, or custom C# programming using the .NET XML namespace. However, I am open to exploring o ...

Focus on targeting dynamic IDs such as #event_rules_attributes_0_interval, #event_rules_attributes_1_interval, etc. using CSS3 styling

Styling input tags using their IDs can be very convenient due to the higher precedence weight they hold compared to classes. For instance, I set a default width for input.text elements within a specific container: .details .metadata input.text { width: 2 ...

Utilizing an offline HTML file instead of a URL within Objective-C programming

Is there a way to modify this code to utilize an offline (bundled) file instead of the "http://5times9.com"? - (void)loadAddWeb { UIWebView *addWeb = [[UIWebView alloc] init]; NSURL *webURL = [NSURL URLWithString:@"http://5times9.com"]; NSURLRequest *we ...

Instructions on using $.post in jquery to sude uploaded file to a php file for processing

Is there a way to upload photos asynchronously using $.post without relying on .serializeArray()? I want to send a form, containing an upload file, to a PHP processing file. Any suggestions? HTML: <form action="upload1.php" method="post" id="usrform" ...

Does the flex-grow property depend on the flex-basis in flexbox?

Check out this snippet of HTML code <div class="container"> <div class="box box1">one ...

Regular expression for matching a CSS definition that includes a specific name and value

Looking to match all CSS definitions with the name "test" and color set to gray? Here are a few examples: a.test { color: grey; } or .test { color: grey; }, as well as a.test:after, a.test { font-size: 10px; color: gray; } It's important to consider ...

Tips on ensuring that PRE elements expand to fit column size without growing when additional data is added

I am facing a design challenge where I have a main card (1) that contains two sub-cards (2 and 3) using Bootstrap columns. Sub-card number 2 adjusts its height dynamically based on the screen size, while sub-card number 3 consists of a code snippet element ...

Error encountered while attempting to convert the varchar value to an integer data type

I am facing an issue with my GridView. Inside the gridview, I have a hyperlink where I am attempting to pass a query-string to another page. The ID is of type INT but for some reason, I am encountering this error: Conversion failed when converting the v ...

Interacting with shadow DOM elements using Selenium's JavaScriptExecutor in Polymer applications

Having trouble accessing the 'shop now' button in the Men's Outerwear section of the website with the given code on Chrome Browser (V51)'s JavaScript console: document.querySelector('shop-app').shadowRoot.querySelector ...

Show unique language text in the <noscript> element based on the user's browser language

I am just starting out with HTML, and I would like to show a message if JavaScript is disabled. To do this, I have placed the message within the <noscript> tag, which is working perfectly. <noscript> Need to enable Javascript. </noscript> ...

The drop-down menu is malfunctioning as it is not displaying the complete text and the

At the moment, I am facing an issue where my submenu items for Add Subject, Drop Subject, and Delete Subject are not centralized as intended. Additionally, the Executive Summary text in the submenu is getting cut off. Can someone please help me with expand ...