Utilizing Selenium WebDriver to extract links located within numerous Span tags

I am looking to count and list the number of links within different Span Tags. Within the provided HTML code snippet, there are two anchor links under 'Upcoming Event' Span, and 4 links under the 'Recent Free LIVE Webinars' section.

Can someone please provide an Xpath query to extract this information?

<!-- language: html -->

<div class="widget widget_text">
<div class="widget-wrap">
<div class="textwidget">
<span style="color: #FF3300;font-size:14px">
<b>Upcoming Event </b>
</span>

    <a href="http://www.itelearn.com/events/mobile-application-testing-   
            career">2nd July 2013:Your Career in Mobile Application Testing </a>

    <a href="http://www.itelearn.com/live-training/qtp-11-live-
            training#">19thJune 2013 : QTP Live Training</a>


    <span style="color: #FF3300;font-size:14px">
             <b>Recent Free LIVE Webinars</b>
             </span>

    <a href="http://www.itelearn.com/events/teachitelenter code hereearn-a-
            mega-webinar">Teach@ITeLearn Initiative </a>

    <a href="http://www.itelearn.com/events/how-to-be-a-software-security-
            testing-expert">How to become a Software Security Testing Expert?</a>

    <a href="http://www.itelearn.com/events/how-to-master-hp-qtp">How to    
            Master QTP?</a>

    <a href="http://www.itelearn.com/events/how-to-master-selenium-video-
            page">How to Master Selenium?     
            </a>

Answer №1

In my opinion, the most straightforward approach would be to identify all the spans within the webpage first. Then, for each span, locate every anchor element it contains. Output the number of links found in each span and proceed to extract and display the corresponding href values.

spans = driver.find_elements_by_tag_name("span")
for span in spans:
    links = span.find_elements_by_tag_name("a")
    print(str(len(links)));
    for link in links:
        print(link.get_attribute("href"));

Answer №2

Here is a suggestion for locating elements using XPATH:

//To find the 'Upcoming Event' span ..
 XPATH = "//span//b[text()='Upcoming Event']//a"
//To locate the 'Recent Free LIVE Webinars' span ...
 XPATH="//span//b[text()='Recent Free LIVE Webinars']//a"

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

Can anyone recommend a speedy sorting algorithm for an extensive list of objects in JavaScript?

Struggling to organize a large array of 2000 elements in ReactJS using JavaScript. The array includes: data = [ { index: 0, id: "404449", product_name: "ette", brand_name: "Dyrberg/Kern", base_pri ...

The TypeScript inference feature is not functioning correctly

Consider the following definitions- I am confused why TypeScript fails to infer the types correctly. If you have a solution, please share! Important Notes: * Ensure that the "Strict Null Check" option is enabled. * The code includes c ...

What is the process for implementing a version folder for my @types/definition?

Is there a way to access the typings for react-router in my project? My package.json file currently has this dependency: { "@types/react-router": "^4.0.3" } However, it seems to be using the standard index.d.ts file from DefinitelyTyped library which i ...

Implementing image rendering functionality in Vue.js

So here's what's going on: I developed a horror movie bucket list app for my bootcamp final project. The minimum viable product received positive feedback, and I obtained my certification. However, now that I've graduated, I want to enhance ...

Issue with Attaching Click Event to Dynamic Div Elements

I've implemented divs with a Click Event triggered by a value entered in a text box. See an Example Here Upon opening the page, clicking any rows will trigger an alert. However, if you change the value in the text box (Enter Number) and click load, ...

Mastering the Rejection of Promises in Javascript with Graceful Elegance

One effective pattern using ES2017 async/await involves: async function () { try { var result = await some_promised_value() } catch (err) { console.log(`This block will be processed in a reject() callback with promise patterns, which is far mo ...

What could be causing the issue with the navigation circles on my carousel not updating when clicked?

I created my own carousel from scratch and it's working perfectly fine except for one issue - clicking on the navigation circles. When using the interval/infinite loop prop, the circles update to the correct active slide as expected. The same goes fo ...

Is it possible to insert clickable links within the content of a Twilio text message?

Currently, I am utilizing Twilio and Express to send programmable SMSs to the users of my web application. I'm curious if it's possible to include hyperlinks within the body of these text messages. Is there a method to achieve this? I have attem ...

AngularJS's $resource module returns an empty array as a response

I am trying to display a table with items from JSON data. I have created a Service that returns the JSON data. In my controller, I am querying the Service to receive an array of data. It's a little confusing because I am putting the response in a new ...

Is it possible to change the background color of the scrolling page?

Let's say I have 10 different sections, each with a unique background color assigned to them. As the user scrolls down from section 1 through 10, my goal is to dynamically change the body tag's background color based on which section is currentl ...

Is it feasible to maintain a variable as a reference across views while utilizing ng-view?

I am facing a unique challenge: I have a webpage with two tabs that need to utilize ng-view from AngularJS. The twist is that both tabs must share the same variable, similar to referencing a variable in C# using the "ref" keyword. If you want to see an ex ...

No data is being retrieved by SWR

I'm struggling to make SWR work in my code. Despite trying multiple examples, I can't seem to get it functioning properly. It's frustrating because the code looks fine and should work. I feel like I must be missing something simple. Current ...

Introducing the World of Wordpress Blogging

How can I create an introduction on my WordPress site similar to the one found at ? I am specifically interested in incorporating the expanding horizon line effect. It seems like it may just be a GIF that plays and then fades into the homepage. Are there ...

Using a <button> tag instead of a <div> inside of a <button> is not allowed, as clickable divs are typically frowned upon. What

I'm currently developing a React App that functions as a calendar, allowing users to click on specific days displayed as large squares to view information for that day. For accessibility purposes, I initially considered using <button> elements b ...

Click event from Angular Material menu is triggered twice on mobile devices

After implementing a basic angular material side menu from the demo, I noticed that all click events are being fired twice on the entire page when viewed in mobile browsers. This issue can even be replicated in the Chrome emulator. (To reproduce, enable th ...

A guide on accessing the JSON file data using jQuery

Currently, I am attempting to retrieve data from an API. Essentially, there is a URL that returns JSON data which I need to utilize. var url = "http://212.18.63.135:9034/played?sid=1&type=json"; $.getJSON(url, function(data) { console.log(data) ...

Displaying an array of JSON objects in ReactJS by parsing them

Recently, I've encountered an odd issue with my React App. I am attempting to parse a JSON object that includes arrays of data. The structure of the data looks something like this: {"Place":"San Francisco","Country":"USA", "Author":{"Name":"xyz", "Ti ...

Analyzing and swapping objects within two arrays

Looking for a better way to append an array of objects customData to another array testData? If there are duplicate objects with the same name, replace them in the resulting array while maintaining their order. Here's my current approach in ES6 - any ...

Simulating service calls in Jest Tests for StencilJs

When testing my StencilJs application with Jest, I encountered an issue with mocking a service class method used in a component. The service class has only one function that prints text: The Component class: import {sayHello} from './helloworld-servi ...

Why does the implementation of my interface differ from what is specified in the TypeScript documentation?

Currently delving into the world of TypeScript documentation https://www.typescriptlang.org/docs/handbook/2/classes.html Specifically focusing on the section implements Clauses, an interesting revelation surfaces: A Word of Caution It’s worth noting t ...