简体   繁体   English

完成循环后再次需要在 python 中运行相同的循环

[英]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.在此循环之后必须单击此driver.find_element_by_xpath('.//button[@class="action-next"]').click()再次继续相同的循环。

Note: The web table has 5 paginations and each page has few negative values, I'm trying to get negative values from all pages.注意:web 表有 5 个分页,每个页面都有很少的负值,我试图从所有页面中获取负值。

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?只需一个简单的 function 换行,每次需要时调用它,如果我理解正确,您需要单击某种“下一页”按钮并继续,对吗?

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.或者只是嵌套在 for/while 循环中。 Why not?为什么不?

Try this to find all pages until neither 'QuantityData' nor 'action-next' was not found.尝试此操作查找所有页面,直到未找到“QuantityData”和“action-next”。 First time seeing selenium, but their document suggests using 'NoSuchElementException'.第一次看到 selenium,但他们的文件建议使用“NoSuchElementException”。

from selenium.common.exceptions import NoSuchElementException

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

If I understand correctly you will need a function.如果我理解正确,您将需要 function。 Neat when you need to do the same thing several times.当你需要多次做同样的事情时,整洁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM