0
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.

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Sorry did not see the answer over btw.
1

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

2 Comments

No, I have positive values also if I use exception it'll not print next values. And your 1st code is useful for me but I have 48 pages to find negative values in selenium python.
This won't print if value is positive. Exception only happens if there was no elements, not negative values. I don't know how your web table looks like, so assuming last page don't have next button. Even in that case, for loop completes before finding button. Therefore, prints works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.