Bronson77
Bronson77

Reputation: 299

Break Loop After No More Pages Python

I'm paginating through orders inside Shopify. I'd like to break out of the loop once it hits the last page. The button that I use to click to paginate is still active and clickable on the last page the only thing different is that it has a property of "disabled". Typically, I would look for the "next" button and if isn't there, then loop will break. Not sure what to do in this case.

Here's my code that I know doesn't work.

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

Button 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>

Upvotes: 0

Views: 135

Answers (2)

KunduK
KunduK

Reputation: 33384

Try the below code.Check the length count >0 then break else click.

 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()

Upvotes: 0

supputuri
supputuri

Reputation: 14145

Here is the logic that you should use.

    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()

Upvotes: 0

Related Questions