我使用 selenium 在 python 中撰寫了一個腳本。
基本上,我打開一個地址檔案,并嘗試在 ArcGis 站點上找到與每個地址關聯的資訊層。
for element in AdressesList:
adress = element
try:
driver = webdriver.Chrome('D:\downloads\chromedriver_win32\chromedriver')
driver.get("https://www.arcgis.com/home/webmap/viewer.html?layers=c9fa1db72f74433b8eea48ed82a4e4f9")
time.sleep(6) // All sorts of things are loaded in the website
search_bar = driver.find_element_by_id("geocoder_input")
search_bar.clear()
search_bar.send_keys(adress)
search_bar.send_keys(Keys.RETURN) //search address
time.sleep(3) // All sorts of things are loaded in the website
mouse.move(422, 516, True, 0.1)
mouse.click('left') // make the element show up
stat_11 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[3]/div[5]/div[2]/div[1]/div[3]/div[3]/div[1]/div[2]/div/div/div[2]/div[3]/table/tr[2]/td[2]/span").text // The specific webelemnt xpath within which the information I need is stored
driver.close();
driver.quit();
print()
LinesToTxtFile.append("NO" ", " adress.replace(',', '-') ", " str(stat_11))
except:
LinesToTxtFile.append("An, " "exception, " "occurred")
這就是所有相關部分。我想要做的,但我還沒有找到答案,是重復迭代到當前元素,以防代碼拋出例外。
那是,
// try block here
except:
LinesToTxtFile.append("An, " "exception, " "occurred")
RepeatCurrentIterationAgainWithTheSameElement() // This is for illustration purposes only
謝謝!
uj5u.com熱心網友回復:
代碼完成后添加一個while True和break:
for element in AdressesList:
adress = element
while True:
try:
driver = webdriver.Chrome('D:\downloads\chromedriver_win32\chromedriver')
driver.get("https://www.arcgis.com/home/webmap/viewer.html?layers=c9fa1db72f74433b8eea48ed82a4e4f9")
time.sleep(6) // All sorts of things are loaded in the website
search_bar = driver.find_element_by_id("geocoder_input")
search_bar.clear()
search_bar.send_keys(adress)
search_bar.send_keys(Keys.RETURN) //search address
time.sleep(3) // All sorts of things are loaded in the website
mouse.move(422, 516, True, 0.1)
mouse.click('left') // make the element show up
stat_11 = driver.find_element_by_xpath("/html/body/div[1]/div[1]/div[3]/div[5]/div[2]/div[1]/div[3]/div[3]/div[1]/div[2]/div/div/div[2]/div[3]/table/tr[2]/td[2]/span").text // The specific webelemnt xpath within which the information I need is stored
driver.close();
driver.quit();
print()
LinesToTxtFile.append("NO" ", " adress.replace(',', '-') ", " str(stat_11))
break
except Exception:
LinesToTxtFile.append("An, " "exception, " "occurred")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/404985.html
標籤:
