Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select specific row in html table - try out these ideas to do (checking text or attribute) #1086

Closed
andrei152515 opened this issue Jul 22, 2021 · 14 comments
Assignees
Labels

Comments

@andrei152515
Copy link

Hi, i'm new to tagui and i'm trying to select a specific row in an html table.
I have a table on a website and i want to delete a specific row where a specific class (text-danger) is present.
example below.
image

I want to delete the specific products which are out of stock.
for the example shown the html code is:
image

Can someone help me please ?
Thank you :)

@kensoh kensoh self-assigned this Jul 23, 2021
@kensoh
Copy link
Member

kensoh commented Jul 23, 2021

Hi @andrei152515 an easy way would be using read step to get the text into a variable, then check before deleting.

read xpath_of_item_description to description
if description contains '***'
    click xpath_of_button_to_delete

Above can then be wrapped in a for loop to process all the records, using this XPath syntax (xpath)[n]. Other ways that can be explored will be reading attributes using xpath/@class or check if xpath//[@class="text-danger"] exists using exist().

@kensoh
Copy link
Member

kensoh commented Jul 23, 2021

Other than visually inspecting the HTML code, Chrome extension like SelectorsHub can be very helpful to find XPath.

@kensoh kensoh changed the title select specific row in html table Select specific row in html table - try out these ideas to do (checking text or attribute) Jul 23, 2021
@kensoh kensoh added the query label Jul 23, 2021
@andrei152515
Copy link
Author

Hi @kensoh
Thank you for your quick response, your suggestions helped me a lot, but now I face another issue.
I get the number of products from that list (products from the cart) into a variable using count and after that I used 'for' to loop from 1 to the number of products then delete the product from the cart, where exist ''
The issue is that every time I delete a product, the list is updated and for example if I have 3 products that are out of stock (having the *** mark) from a list of 4 total products:
#1 out of stock product

#2 out of stock product***
#3 product in stock
#4 out of stock product***

If I delete the first product, tagui will not delete the second one, because #2 will become #1 and the for loop at that time is passed from the step #1 which delete the first out of stock product and in this way I will get only 2 products deleted instead of 3.
Is there any way to deal with this kind of issue?

Thank you so much.

@kensoh kensoh assigned ruthtxh and unassigned kensoh Jul 26, 2021
@kensoh
Copy link
Member

kensoh commented Jul 26, 2021

Hi @andrei152515 I'm bringing in my team-mate @ruthtxh to look at this. This is an interesting scenario, I thought if more people can look into this a better solution can be found.

@ruthtxh
Copy link
Collaborator

ruthtxh commented Jul 26, 2021

Hi @andrei152515
You may create your own counter variable to track and decrement the total count once you delete something. Usually for list of products the xpath will be in an array, so create a loop with variable n to access that array. For example:

counter = count('xpath_of_products')
for (n=1; n<counter; n++)
    read xpath_of_item_description[`n`] to description
    if description contains '***'
        click xpath_of_button_to_delete[`n`]
        js n--
        js counter--

@kensoh
Copy link
Member

kensoh commented Jul 26, 2021

This is a great idea - I'm not sure if the for loops in TagUI can accept direct decrements of the variables, but is worth trying!

@andrei152515
Copy link
Author

andrei152515 commented Jul 27, 2021

hi @ruthtxh, thank you for your suggestion, but is not working. The reason, I think, is because 'for' is using the first count and do not take into account the decrementing. Below I will attach the code that I'm using, and the results, to have a better idea of what I'm trying to do:

number_items=count('//[@Class="table table-bordered"]//[@Class="text-center"]') - 1
echo number_items

for (item=1; item<number_items; item++)
if exist ('//[@id="content"]/form/div/table/tbody/tr[item]/td[2]//[@Class="text-danger"]')
click (//*[@id="content"]/form/div/table/tbody/tr[item]/td[4]/div/span/button[2])
echo the number of items before decrement is: number_items
js item--
js number_items--
echo the number of items after decrement is: number_items

The total number of items in the list is 5, and 3 of them are out of stock.
image

What about to read all the rows where I have "***" and after I have all the information, in variables, delete the specific rows, in the end?

@marcelocecin
Copy link

@andrei152515 hello, can you share the website url?

@andrei152515
Copy link
Author

hi @marcelocecin, unfortunately is on localhost

@marcelocecin
Copy link

and the html code?

@andrei152515
Copy link
Author

andrei152515 commented Jul 27, 2021

The website is based on Opencart. This is the cart page:
website.txt

@marcelocecin
Copy link

marcelocecin commented Jul 27, 2021

try to use this XPath //span[text()="***"]/../..//td//div//span//button[@title="Remove"]

total_items = count('//span[text()="***"]')
for n from 1 to total_items
  click //span[text()="***"]/../..//td//div//span//button[@title="Remove"][`n`]

@kensoh
Copy link
Member

kensoh commented Jul 28, 2021

Hi Marcelo, this looks like a brilliant solution!

If the removal has some processing time, @andrei152515 can add a wait after deleting for page to refresh before loop again.

@andrei152515
Copy link
Author

andrei152515 commented Jul 28, 2021

hi @marcelocecin, very good idea, helped me a lot, with just a small rectification:

total_items = count('//span[text()=""]')
for n from 1 to total_items
click //span[text()="
"]/../..//td//div//span//button[@title="Remove"] [1]

For, 'click', instead of n is working with 1. If there is n then I receive this error: ERROR - cannot find //span[text()=""]/../..//td//div//span//button[@title="Remove"][2]
and I think this is because after the first product with ("
") is deleted, n becomes 2 and the second product from the deletion list become 1 and so on. The product to delete from the list is always 1.

Thank you.

@kensoh kensoh closed this as completed Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants