Steps for retrieving the attribute values of <i> tags enclosed within a designated <div> element

Currently, I am extracting data from a website where I encounter a recurring pattern with a varying number of values.

For example, an instance of this pattern is shown below:

<div class="lang">
<i class="flag fr" qtip-tooltip="Français"></i>
<i class="flag nl" qtip-tooltip="Néerlandais"></i>
<i class="flag gb" qtip-tooltip="Anglais"></i>
<i class="flag it" qtip-tooltip="Italien"></i>
</div>

I want to extract a list containing all the qtip-tooltip values in a string or a list format for each div. Is there a way to achieve this?

I attempted the following code:

langs = driver.find_elements(by=By.XPATH,value='//div[@class="lang"]') 

However, this resulted in empty string values. Can anyone assist me with this problem?

Answer №1

You're almost there to reaching your objective - to accomplish it, simply loop through the ResultSet of langs, locate all <i> elements within each <div>, and extract their attributes as you iterate again:

langs = driver.find_elements(By.XPATH,'//div[@class="lang"]')
for lang in langs:
    tooltips = [l.get_attribute('qtip-tooltip') for l in lang.find_elements(By.XPATH,'.//i')]
    
    ## as list
    print(tooltips)
   
    ## as comma separated string
    print(','.join(tooltips))
Output
['Français', 'Néerlandais', 'Anglais', 'Italien']

or

Français,Néerlandais,Anglais,Italien

To obtain languages across all div as a single list or better yet, a set with unique values, you could use:

set(l.get_attribute('qtip-tooltip') for l in driver.find_elements(By.XPATH,'//div[@class="lang"]/i'))

Output

{'Anglais', 'Français', 'Italien', 'Néerlandais'}

Answer №2

Give this a shot:

sections = driver.find_elements_by_css_selector('div.section')

You can then iterate through your sections by looping over your variable:

for section in sections:
  print(section)

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 scrubbing through videos, Chrome consistently throws a MEDIA_ERR_DECODE error

Encountering a MEDIA_ERR_DECODE error while scrubbing in Chrome on two different PCs. One PC runs Windows 7 with Chrome version 26, and the other runs Windows 8 with Chrome version 27. This issue occurs across all videos. When attempting to scrub on the v ...

Trouble obtaining AJAX result using onClick event

As a newbie to AJAX, I am still trying to grasp the concept. My task involves using an AJAX call to extract specified information from an XML file. Even after carefully parsing all tag elements into my JavaScript code, I encounter a problem when attempting ...

The onclick function fails to function properly following an Ajax reload of the <div> element

I have an issue with my onclick function that only works the first time. Every time the onclick function triggers an ajax request, it successfully reloads a div which contains code to retrieve values from SQL and build an HTML table using a for loop. Alth ...

Navigating to a different tab or window in Selenium that presents XML-style formatting instead of the typical web-view

There is a situation where, upon clicking a button on a website, it opens a new page in a separate tab. This new page is not your typical webpage, and the standard switchTo().window() function does not work as it shows an error message stating "Web view no ...

Is there a way to parse this source text using selenium in Python?

<iframe id="frameNewAnimeuploads0" src="http://www.watchcartoononline.com/inc/animeuploads/embed.php?file=rick%20and%20morty%2FRick.and.Morty.S02E10.The.Wedding.Squanchers.720p.WEB-DL.DD5.1.flv&amp;hd=1" width="530" height="410" frameborder="0" scro ...

Switching effortlessly between Fixed and Relative positioning

As I work on creating a unique scrolling experience, I aim to have elements stop at specific points and animate before returning to normal scroll behavior once the user reaches the final point of the page. Essentially, when div X reaches the middle of the ...

Tips for managing the windows download notification in IE11 with Selenium Webdriver without relying on AutoIT

During testing on IE11, a situation arises where clicking on the download link for a document results in a new blank window opening with save and cancel options. I prefer to click cancel, return to my original window, and continue with validation tasks. ...

What could be the reason for the malfunctioning dropdown menu in my navigation bar upon clicking it?

After spending hours practicing creating a navbar using Bootstrap 3.3.7, I've hit a roadblock - the dropdown is not working when clicked on. It's frustrating because I have double-checked all my scripts and ensured that I have the latest version ...

Get permission to view a parent's list of items

Currently, I have a diagnostic class implemented as a @dataclass in Python. This class includes a method that corresponds to the level of the diagnostic message (e.g., info, warning, error), along with an enumeration named Level: @dataclass class Diagnost ...

When using the .after() method to add jQuery elements, keep in mind that they will not trigger any

Here is the snippet of code provided: <s:file name="upload" id="upload"></s:file> $('input[id^="upload"]').change(function(){ alert("aa"); $(this).after('<input type="file" name="upload_3" id="upload_3"/> ...

Optimal method for extracting PyTables table with column names stored as variable names

I have a piece of code that fulfills my requirements, but it takes more than 10 seconds to run on a table with 200 variables and 64000 rows. Is there a faster way to create a variable namespace that matches the column names? strExec = "a = table[:]" for ...

Discover the dictionary key linked to a specific value in Python

My task is to look up the value of two elements in a dictionary rather than their key. The code I currently have does not search for the value of the dictionary: for key in val["timeSlot"]: for value in val[key]: if test_date in value: ...

What is the best way to implement a loading cursor when the Submit button is clicked?

Is there a way to incorporate a progress cursor into my code in order to notify the user to wait when they click the Submit button or the Upload Button while uploading multiple files? Below is an example of my form: <form action="" method="post" enct ...

The debugger in Python offers both step-through and free-running modes, allowing developers to navigate through code execution along distinct paths

After running the script import sys if sys.gettrace(): print 'OK' else: assert False, 'good grief' using this command % python -mpdb bugdemo.py (With Python v. 2.7.8) The first prompt displayed is: -> import sys (Pdb) I ...

JavaScript can retrieve the default page name that is hidden within the browser's URL

When a URL is entered without a specific file name at the end, such as "www.google.com," the server typically serves a default file like "index.html" or "default.aspx." In this scenario, if the browser displays "www.google.com," I am looking to extract the ...

Step-by-step guide on choosing a dropdown menu within a submenu using Selenium WebDriver

I am a beginner with Selenium and I'm attempting to choose an option (Job Title) from the sub menu (Job) under the main menu 'Admin' on OrangeHRM website. When running my script, it correctly clicks on Admin but instead of hovering over "Jo ...

Two neighboring sections containing dynamic elements. One section automatically adjusts its size to fit the content, while the other allows

I need to display 2 adjacent boxes with dynamic content rendered using Angular. The Container should have the same height as Box1. The height of Box2 may vary due to its dynamic nature, but it should never be taller than Box1. If it does become higher, a s ...

Determine if the webpage is the sole tab open in the current window

How can I determine if the current web page tab is the only one open in the window? Despite searching on Google for about 20 minutes, I couldn't find any relevant information. I would like to achieve this without relying on add-ons or plugins, but if ...

Unlocking the synergy between Python and React through the seamless integration of component-based

I am attempting to containerize the workflow of an isomorphic app using Docker. This is the Dockerfile I used: FROM python:3.5-slim RUN apt-get update && \ apt-get -y install gcc mono-mcs && \ apt-get -y install vim ...

When saving data to a CSV file in Python, the information is separated by commas, ensuring each character is properly recorded

Trying to read data from a CSV file and then write it to another one. However, the written data is separated by commas. Below is the code snippet: with open(filenameInput, 'r') as csvfile: dfi = pd.read_csv (filenameInput) dfi - dfi.ilo ...