Priyanka Tester
Priyanka Tester

Reputation: 25

After completed loop again need to run same loop in python

for element in driver.find_elements_by_xpath('.//span[@data-bind = "text: $salableQuantityData.qty"]'):
    elem = element.text
    stock = int(elem)
    if stock < 0 :
        print(stock)

After this loop have to click this driver.find_element_by_xpath('.//button[@class="action-next"]').click() again continue the same loop.

Note: The web table has 5 paginations and each page has few negative values, I'm trying to get negative values from all pages.

Upvotes: 0

Views: 130

Answers (2)

jupiterbjy
jupiterbjy

Reputation: 3503

Just a simple function wrap, and call it every time you need it, if I understood correctly you need to click some sort of 'next page' button and continue, right?

def some_work():

    for element in driver.find_elements_by_xpath('.//span[@data-bind = "text: $salableQuantityData.qty"]'):
        elem = element.text
        stock = int(elem)
        if stock < 0 :
           print(stock)

    driver.find_element_by_xpath('.//button[@class="action-next"]').click()


some_work()

or just nest in for/while loops. Why not?

Try this to find all pages until neither 'QuantityData' nor 'action-next' was not found. First time seeing selenium, but their document suggests using 'NoSuchElementException'.

from selenium.common.exceptions import NoSuchElementException

while True:
    try:
        some_work()
    except NoSuchElementException:
        break

Upvotes: 1

Stian Roll
Stian Roll

Reputation: 39

If I understand correctly you will need a function. Neat when you need to do the same thing several times.

Upvotes: 1

Related Questions