如果找不到此site的元素,我正在尝试继续工作: 我尝试过使用:
try:
except NoSuchElementException:
pass
以及if then
陈述。但是,前一种方法不起作用,如果方法似乎很容易破解。
我选择了这个page没有文字,看看它如何处理不存在元素的页面。
try:
# Team ODDS
time.sleep(4)
clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ('.brdSports td td tr:nth-child(1) .sport-namerow .left'))))
langs = driver.find_elements_by_css_selector(".brdSports td td tr:nth-child(1) .sport-namerow .left")
#tbody > tr > td > table > tbody > tr: eq(1) > td > table > tbody > tr > td > table > tbody > tr > td:eq(1) > table > tbody > tr: eq(0) > td > table > tbody > tr > td > table > tbody > tr > td:eq(0) > div > div
langs_text = []
for lang in langs:
print(lang.text)
langs_text.append(lang.text)
time.sleep(0)
time.sleep(4)
# BACK TEAM
#langs1 = driver.find_elements_by_xpath("//ul[@class='runners']//li[2]")
clickMe = wait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ('.brdSports td td tr:nth-child(1) .sport-namerow .txtBld'))))
langs1 = driver.find_elements_by_css_selector(".brdSports td td tr:nth-child(1) .sport-namerow .txtBld")
langs1_text = []
for lang in langs1:
print(lang.text)
langs1_text.append(lang.text)
url1 = driver.current_url
time.sleep(0)
with open('vtg12.csv', 'a', newline='', encoding="utf-8") as outfile:
writer = csv.writer(outfile)
for row in zip(langs_text, langs1_text):
writer.writerow(row + (url,))
print(row + (url,))
except NoSuchElementException:
pass
完整code
答案 0 :(得分:1)
根据您在评论中发布的堆栈跟踪,您需要为Selenium TimeoutException
添加其他例外。
这应该有效:
except TimeoutException:
pass