繁体   English   中英

在没有更多页面Python之后打破循环

[英]Break Loop After No More Pages Python

我正在通过Shopify内部的订单进行分页。 一旦它到达最后一页,我想打破循环。 我用来点击分页的按钮仍处于活动状态,并且可以在最后一页上单击,唯一不同的是它具有“禁用”属性。 通常,我会寻找“下一步”按钮,如果不存在,那么循环将会中断。 不知道在这种情况下该怎么做。

这是我知道的代码不起作用的代码。

while True:
    links = [link.get_attribute('href') for link in driver.find_elements_by_xpath("//*[@testid='Item-Content']/div[2]/div/div/span/span/a")]

    try:
        driver.find_element_by_xpath('//*[@id="AppFrameMain"]/div/div/div[2]/div/div[1]/div/div/div[3]/nav/span[2]/button').click()
        time.sleep(5)
    except:
        pass

按钮HTML

<button type="button" class="p_2bbL9" aria-label="Next" tabindex="0" aria-describedby="TooltipContent304" disabled>
  <span class="p_2-hnq">
    <svg viewbox="0 0 20 20" class="p_v3ASA" focusable="false" aria-hidden="true">
    <path d="M17.707 9.293l-5-5a.999.999 0 1 0-1.414 1.414L14.586 9H3a1 1 0 1 0 0 2h11.586l-3.293 3.293a.999.999 0 1 0 1.414 1.414l5-5a.999.999 0 0 0 0-1.414" fill-rule="evenodd"></path>
    </svg>
  </span>
</button>

这是你应该使用的逻辑。

    while True:
    # your logic goes here
    links = [link.get_attribute('href') for link in
           driver.find_elements_by_xpath("//*[@testid='Item-Content']/div[2]/div/div/span/span/a")]
    # exist loop if next button is disabled
    if not(driver.find_element_by_xpath("//button[@aria-label='Next']").is_enabled()):
        break
    else:
        driver.find_element_by_xpath("//button[@aria-label='Next']").click()

尝试下面的代码。检查长度计数> 0然后打破其他点击。

 while True:
    if len(driver.find_elements_by_css_selector('button[aria-label="Next"][disabled]'))>0 :
       break
    else:
        driver.find_elements_by_css_selector('button[aria-label="Next"]')[0].click()

暂无
暂无

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

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