[英]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.