Executing a script for every row in a table using Python and Selenium

As a newcomer to Python and Selenium, I have been struggling with a specific task for days now. Despite my efforts to search and experiment, I find myself completely stuck.

My goal is to access sales records within a table that contains information about each order. The challenge lies in clicking on the "View sales record" option for every sale on the page and opening each record in a different tab.

The only code that seems to work for me so far is:

driver.execute_script("document.querySelector('#LineActions > div > div > div.mu-ov-w.mu-d-ov > div > div.mu-ov-c1 > div > div > table > tbody > tr > td > ul > li:nth-child(3) > a').click();")

However, this script only opens the sales record for the first order. I am unsure of how to proceed to open records for all other orders.

The tables displaying sales records are structured as follows:

<table id="tbl_mu_active_tbl_id"....>
    <thead class="dt-cs" id="tbl_mu_active_tbl_id_h_0">
        ....
    <tbody class="dt-rs dt-cs">
        <tr class="dt-sh dt-slb dt-fr" id="mu_active_tbl_id_0">
        ....
    <tbody class="dt-nsb">
        ....
    <tbody class="dt-rs dt-cs">
        <tr class="dt-sh dt-slb dt-fr" id="mu_active_tbl_id_1">
        ....

The rows containing relevant information increment their IDs by one. For example,

#tbl_mu_active_tbl_id > tbody:nth-child(2)
refers to the first row, while
#tbl_mu_active_tbl_id > tbody:nth-child(4)
pertains to the second row, and so forth.

I have attempted various methods to iterate through these rows but have not been successful in executing the script for any other row besides the first one.

Therefore, my current questions are:

How can I ensure the script runs on each row of the table?

Is there a way to modify the script to open the link in a new tab?

Answer №1

If you want to run a script on each row of a table, the first step is to gather all elements within that table and store them in an array. Then, simply loop through the array. This task can be easily accomplished using the find_elemens_by_xpath method like this:

table = driver.find_element_by_id('tbl_mu_active_tbl_id')
rows = table.find_elements_by_xpath('//tbody')  #Find all tbody elements inside the table

Next, iterate through the rows array:

If you need help with opening a new tab, check out this resource: Open web in new tab Selenium + Python

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys

actions = ActionChains(driver)
for i in rows:
    actions = ActionChains(driver)
    actions.key_down(Keys.CONTROL).click(i).key_up(Keys.CONTROL).perform()

It's important to proceed with caution when dealing with new tabs as switching to the new tab window to continue executing scripts may pose a challenge. For guidance on handling this situation, refer to: Selenium webdriver using switch_to_windows() and printing the title doesn't print the title.

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

Using JavaScript to ensure that a div is not hidden on page load if a checkbox is unchecked

Upon inspecting a page, I am implementing a script to check if a checkbox is selected. If not selected, the goal is to hide a specific div element. While troubleshooting this issue, I suspect the problem may be due to the lack of an inline element within t ...

After attempting to refresh the webpage with the F5 key, I noticed that the jQuery progress bar was not functioning properly. Additionally, the webpage failed to display any

Trying to implement a web loading bar using jQuery on my website was initially successful, but I encountered an issue when reloading the page with F5. The progress bar would not work and the webpage displayed only a white background. Below is the code snip ...

Encountering an inexplicable "undefined" error in HTML after receiving an

(apologies for any misspelled words) Hey everyone, I hope you're having a great time. This is my first experience with ajax, and I'm trying to incorporate some elements (messages sent and received) into HTML using ajax. However, all I see is an u ...

Is there a way to embed an AJAX submit form into a notification without the need for page refresh?

I have integrated the jQuery plugin toastr js () for notifications on my website. I am facing an issue where I want to include a contact form within the notification popup and submit it using AJAX. Despite having the form working fine outside of the toastr ...

Skipping every third index in a JQuery .each loop after removing an element

I have a project where I need to display elements in rows of 4, and when an element is deleted, I want the remaining elements to shift up. To achieve this, I am currently assigning a class "last" to every fourth item after a deletion and inserting a spacer ...

Is there a way to activate the width styling from one class to another?

I have these 2 elements in my webpage: //1st object <span class="ui-slider-handle" tabindex="0" style="left: 15.3153%;"></span> //2nd object <div id="waveform"> <wave style="display: block; position: relative; user-select: none; he ...

Determining the visible dimensions of an iframe

Is there a way to determine the visual size inside an iframe using JavaScript or jQuery? In this scenario, only a portion of the iframe is displayed. The iframe itself has dimensions of 300x300, but it is being limited by a div to 300x100. <div style= ...

Simple 3-column grid design in HTML and CSS

My task is to design a 3-grid layout that resembles the following structure: ------------------------------------- | Image | The name of logo | > | ------------------------------------- I attempted this using the code below: <div class=" ...

Using the Play framework to showcase data in an HTML table

I have a project where I am working with multiple tables, one of which is a Person table containing id and name attributes. I have successfully retrieved the data from the database using MySql and converted it into JSON. However, I am struggling to find a ...

How can I disable image loading in Internet Explorer using Selenium and Java?

In an effort to enhance the efficiency of our automation process, I am looking into disabling image loading. I have managed to find a solution for Firefox and Chrome (Reference: Selenium WebDriver/Firefox|Chrome/Java How to disable image loading), but not ...

The certificate verification process encountered an error: unable to retrieve the local issuer certificate

I am seeking assistance for an issue I encountered while installing a module. An error message certificate verify failed: unable to get local issuer certificate appeared. Upon attempting to resolve this by running the Install Certificates.command file in ...

Retrieve the content of a different page and store it as a variable using ajax

Is it possible to assign the content of another HTML page to a JavaScript variable? I attempted: var X = $(http://www.website.com/home).html() Unfortunately, this did not work, even though it seemed like a good idea. Can anyone provide guidance on how t ...

Tips for wrapping text within a span element that is positioned absolutely

Need a long string for the bullet text as shown in the code, they can't be separate words. Having trouble with overflow auto, it's cutting off the bullet text. The bullet text must remain in absolute position. How can I make the text wrap to t ...

Issue with SciPy installation on Windows: Unsupported wheel format for f.whl on this system

After installing Python 3.6 on my Windows 64-bit machine (which already had Python 2.7), I attempted to pip3 install seaborn, only to realize that scipy was a required dependency that I thought I had installed but apparently did not. I then downloaded the ...

Eliminating Inferior Strategies in Competitive Games

The Challenge Utilizing Gambit's Python API, I am faced with the task of streamlining a game tree by eliminating strictly dominated strategies. The issue arises when my tree becomes too large for the Gambit UI to handle efficiently and save after tri ...

Encountering a RuntimeError when attempting to run a JustPy web app on Jupyter

I've been attempting to run justpy web apps, like the one below, on Jupyter: import justpy as jp def hello_world(): wp = jp.WebPage() d = jp.Div(text='Hello world!') wp.add(d) return wp jp.justpy(hello_world) Unfortunately ...

Using React.js to create table cells with varying background colors

For my React application, I am working with a table that utilizes semantic ui. My goal is to modify the bgcolor based on a condition. In most cases, examples demonstrate something like bgcolor={(condition)?'red':'blue'}. However, I requ ...

The background-size property in CSS does not seem to properly display on iPhones

Hello there! I'm facing an issue with my CSS code when it comes to displaying the image on Safari browser. It works perfectly fine on other browsers, but for some reason on Safari, the image doesn't show up correctly. I tried using a media query ...

The send_keys() function in Selenium version 3.141.0 works perfectly on Windows, but unfortunately, it is not functioning correctly on Linux Debian 11

I've been experimenting with web automation using Selenium. Everything was running smoothly until I updated my packages - now the send_keys() method isn't functioning on Linux, but it's working fine on Windows with the same versions. I&apo ...

Is it possible to produce data on the server and then transmit it to the client?

Can you provide guidance on creating a webpage that prompts a python script to run on the server, and then displays the output of the script back in the browser? I'm put time into researching and seeking assistance during my web programming course. A ...