![](/img/trans.png)
[英]Python, String is printed out more than once after for loop, although I have a break
[英]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.