Comparing the name and URL of a link using Selenium webdriver in C#

I am currently looking for ways to automate the testing of links on a Sharepoint site, ensuring they are connected to the correct URL. However, I have hit a roadblock when it comes to performing the comparison.

After locating the links and adding them to a list, I have been experimenting with different approaches in the if-statement due to my limited experience with Selenium.

IWebElement tableElement = driver.FindElement(By.XPath("//table[@id='skills-table']"));

IList<IWebElement> tableRow = tableElement.FindElements(By.TagName("a"));


String[] rowTD = new String[tableRow.Count];
int i = 0;
foreach (IWebElement element in tableRow)
{

    rowTD[i++] = element.Text;


    if (element.Text.Equals   ) // Need help filling in this comparison

    {


    }
}

Any suggestions or ideas on how to proceed from here would be greatly appreciated.

Answer №1

  1. It is important to note that the

    tableElement.FindElements(By.TagName("a"));
    expression may not capture all nested links in the table. Consider using a relative XPath locator instead:

    IList<IWebElement> tableRow = tableElement.FindElements(By.XPath(".//a"));
    
  2. To retrieve the URL of the link, you can extract the href attribute using the IWebElement.GetAttribute() function

    var URL = element.GetAttribute("href");
    

Answer №2

Thank you so much for your help. I believe I have a solution now, but I would appreciate any feedback on it. Will Assert.ReferenceEquals be able to resolve the issue I'm facing? The test is passing at least.


    IWebElement tableElement = driver.FindElement(By.XPath("//table[@id='skills-table']"));
    IList<IWebElement> tableRow = tableElement.FindElements(By.XPath(".//a"));

    String[] rowTD = new String[tableRow.Count];
    int i = 0;
    foreach (IWebElement element in tableRow)
    {
        rowTD[i++] = element.Text;
        var URL = element.GetAttribute("href");

        Assert.ReferenceEquals(element.Text, URL);
    }

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

What is the best way to connect data so that when a user clicks on a specific card, it appears on a popup card

When a user clicks on any card containing data retrieved from a backend API GET method, that data should be displayed in a pop-up card. In my situation, I have two components: DisplayNotes.vue and UpdateNotes.vue. Whenever a user clicks on a displayed card ...

JavaScript code that functions similarly to VLOOKUP, allowing you to map input values from one field to

As a beginner in HTML and JavaScript, I am trying to create a simple form that automatically populates a specific "Customer Code" when a "Customer Name" is selected from a dropdown list (similar to an Excel VLOOKUP). However, the examples I found on Stack ...

What is the best way to create underlined text that is either left-aligned or centered?

I am looking to replicate this specific style of decoration in a full width container. My goal is to have it aligned to the left of the text and restricted to a maximum width. https://i.stack.imgur.com/H8DXT.png ...

Discovering the art of clicking at random on a webpage using Selenium in Python, along with mastering the techniques of zooming in on a webpage

Is there a way to click randomly within a specific area on a webpage using selenium? I am also curious about how to zoom in on a designated portion of a page, rather than the entire page. For example, if a page is x by y in dimensions, how can one zoom int ...

Press the "show more" button multiple times to display all information in the table and retrieve all data from the table

I am trying to extract all the data from a table on the following page: However, I keep clicking the "show more" button but the table remains limited to showing only 30 rows. import sys import time from pyvirtualdisplay import Display from selenium impor ...

Storing the information received from an API as an HTML element

My HTML file contains JavaScript, along with a URL that displays data retrieved from an AWS lambda API call via AWS API Gateway. The page initially appears blank and the data is structured like this: [ {"user": "bob", "groups": ["bobsGroup"], "policies": ...

A guide on incorporating text into a file utilizing JSP within HTML

I am working on a project that includes a JSP file and a .txt file. The JSP file contains a text field and a button, where the user can enter a text and click the button to store it in the .txt file. I have provided my code below, but I seem to be missin ...

Converting property values to camelCase during JSON serialization in ASP.NET Core

In my SQL Tables, I have definitions of dynamic grids using the AGGrid library for Angular. Here's an example: TB_AGGrid_Definitions ID GridKey 1 TestGrid TB_AGGrid_Columns ID GridId Field 1 1 FirstColumn 2 1 SecondColumn For my Ang ...

Handling errors in Python selenium automation

My Python Selenium script involves running through a loop as shown below... for i, refcode in enumerate(refcode_list): try: source_checkrefcode() except TimeoutException: pass csvWriter.writerow([refcode, 'error' ...

After all other content has been fully loaded, jQuery UI is then loaded onto

For a while now, I've been following a YouTuber's JQuery UI tutorial. Every time I refresh the page, my HTML loads first and then my CSS fills in, causing a momentary flash of HTML and CSS without the UI. Is this normal? Can it be prevented? Her ...

What is the best way to create a new row at a specific index in an ng-repeat loop?

My Goal: I am aiming to insert a new row of ul after every 2 elements in my ng-repeat loop. For example: <ul class="col-sm-2"> <li><p>Automobile & Motorcycle</p></li> ...

Tips for displaying lower quality images while initially downloading higher quality versions

I need to download and display a large image in an img tag. Since the image is not Progressive JPEG, it will render as it downloads. Is there a way to show a low-resolution version of the image while it fetches from the server using only CSS or an HTML p ...

Browser fails to display input value when altered

I have noticed that when the value of the Input element changes, the browser does not display the updated value. However, you can verify this change by inspecting the DevTools in the browser. I am curious as to why the actual value of the Input element is ...

How to effectively combine css() and delay() in jQuery?

fid: https://jsfiddle.net/k13sazuz/ Is there a way to create a delay chain for CSS rules using jQuery? $('.two').css('background','blue').delay(11800).css('background','red'); ...

Access an HTML file in Text Edit on a Mac directly from a web browser

Is there a way to utilize Javascript or another appropriate script to open an HTML file in Text Edit on my Mac? I have created a local web page using Text Edit that has different tabs linking to other Text Edit files within the page. I am looking for a m ...

Limitations on Embedding Videos with YouTube Data API

I have been using the Youtube Data API to search for videos, but I am encountering an issue with restricted content appearing in the results. Specifically, music videos from Vevo are showing up even though I only want videos that can be embedded or placed ...

Generating random user IDs and populating them into a text box using Selenium and Java

For the application I'm working on, I have to create new users frequently. I need a code snippet that can generate usernames using regular expressions. For example, "Alen001", "Alen002", "Alen003" and so forth. driver.findElement(By.id("userId")).sen ...

Having trouble with images not displaying in IE10 on HTML5 canvas?

I recently delved into learning about html5 and the canvas element, attempting to create a basic webpage that rotates an image clockwise and counterclockwise upon button clicks. Although I managed to get it working, when I tested it on ie10, only the blank ...

I am currently attempting to design a blurred background with text overlay, where the text seems to clear up the blur effect of the background. However, I am facing

Goal: I want to achieve a visually appealing effect by overlaying text on a blurred background image, making it appear as though the text is transparent and revealing the clear background image behind it. Status: I have successfully created a layout with ...

Display/Modify HTML form

Looking for advice on how to create an interactive HTML form that displays data and allows users to edit it by clicking 'Edit' before submitting changes. Any suggestions on how to implement this functionality? ...