3

I'm quite new to Python and Selenium and I need help with a few things. I searched and experimented for a few days now but I'm stuck with no ideas what so ever.

Here is what I am trying to do:

I have a table with a bunch of orders. And in there, each sell I've made has a sales record with the buyer's information.

Inside the page with all the sales there is a table that looks as follows :

Once you click on the little devil's arrow you get the following menu:

All I need is a way to click on the " View sales record " option from each arrow on every sale on the page, and to open each sales record in a different tab. ( Every day the number of sales varies )

I tried over 20 different ways to open the sales record link but so far the only thing that seems to work is the following code:

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();")

When the browser loads the page and the script executes it opens the sales record for the first order. The problem is that I have no idea how execute the script for every other order.

Every arrow and link is placed inside a tbody tag. The table looks like this:

<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 tag with 'class="dt-nsb"' is the empty space between each line with 'Sell similar' ( as seen in the first picture )

The id always adds one to the id of the previous relevant tag.

The selector for the first row is #tbl_mu_active_tbl_id > tbody:nth-child(2) For the second one: #tbl_mu_active_tbl_id > tbody:nth-child(4) And so on.

I tried a number of ways for iterating through table rows but I couldn't get the script to execute on none of the other rows.

To sum up, the present questions are:

How to execute the script on each row of the table?

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Lottike
  • 63
  • 5

1 Answers1

1

To execute script on each row of table, you need to find all elements of that table and put them in an array, then just loop. This can be conveniently achieved by using find_elemens_by_xpath as following:

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

Then loop that array rows

For the part of opening new tab, can be found here: 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()

Please be caution when you open new tabs, you will find it hard to switch to new tab window to execute further scripts. Find some clues here Selenium webdriver using switch_to_windows() and printing the title doesn't print the title.

Community
  • 1
  • 1
Phung Duy Phong
  • 876
  • 6
  • 18