繁体   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)

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

注意:web 表有 5 个分页,每个页面都有很少的负值,我试图从所有页面中获取负值。

只需一个简单的 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()

或者只是嵌套在 for/while 循环中。 为什么不?

尝试此操作查找所有页面,直到未找到“QuantityData”和“action-next”。 第一次看到 selenium,但他们的文件建议使用“NoSuchElementException”。

from selenium.common.exceptions import NoSuchElementException

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

如果我理解正确,您将需要 function。 当你需要多次做同样的事情时,整洁。

暂无
暂无

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

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