[英]Using Selenium webdriver with a For Loop
我正在嘗試在網站上為 select 產品 UPC 編寫一個 for 循環。 本質上,我在 Excel 文件中有一個 UPC 列表,我想循環到其中一個:(1)通過搜索欄和 select 找到該項目,或者(2)找不到項目,我想將其添加到列表中未找到的 UPC。 這就是我目前所處的位置。 我不確定是否應該使用 IF ELSE 語句或其他內容。 我真的很感激任何想法或指導。
for upc in upc_list:
driver.find_element_by_xpath('xpath for search bar').clear()
driver.find_element_by_xpath('xpath for search bar').send_keys(upc)
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH, '//*[@id="'+upc+'"]'))).click()
如果未找到該元素,則會引發異常,您必須通過try
except
來捕獲它
upc_list=[]
not_found_list=[]
for upc in upc_list:
driver.find_element_by_xpath('xpath for search bar').clear()
driver.find_element_by_xpath('xpath for search bar').send_keys(upc)
try:
WebDriverWait(driver, 10).until(ec.visibility_of_element_located((By.XPATH, '//*[@id="'+upc+'"]'))).click()
except:
print('no results for '+upc)
not_found_list.append(upc)
continue
print('results for '+upc)
# utc is found
# do what ever you want with it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.