Python 2.7 中的 Try 语句不返回值
Try statement not returning value in Python 2.7
我正在尝试获取社区类型列表并从网站检索信息。但是,try 语句会出现问题。我需要包含 try 语句,因为列表中有可能找不到的社区类型。当我 运行 没有 try 语句的代码时,代码有效。但是当包含时不起作用并且总是 returns 例外。
import selenium ## web retrieval
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains ## needed
comm = ['Colorado Plateau Mixed Low Sagebrush Shrubland','Nowhere Land']
for names in comm:
driver = webdriver.Firefox()
driver.get("http://explorer.natureserve.org/servlet/NatureServe?init=Ecol")
SciName = driver.find_element_by_name('nameSpec')
SciName.send_keys(names)
checkbox = driver.find_element_by_name('noPunct')
checkbox.click() ### unselect box
SciName.submit() ## this submits it
try: SciName = driver.find_element_by_link_text(names) #
except selenium.common.exceptions.NoSuchElementException:
print names
print "Exception found"
driver.quit()
continue
SciName.click() ## enter this bad boy
print "I made it"
driver.quit() ### close the open window
print names
关于为什么会发生这种情况有什么想法吗?我在另一个网页上使用了相同的代码并且工作正常。
打印错误e
returns:
Message: Unable to locate element: {"method":"link text","selector":"Colorado Plateau Mixed Low Sagebrush Shrubland"}`
和
Message: Unable to locate element: {"method":"link text","selector":"Nowhere Land"}
在您尝试查找可以通过添加 time.sleep:
来验证的元素之前,页面尚未加载
SciName.submit() ## this submits it
time.sleep(2)
您会看到第一个成功,但有些仍然失败,因为您得到 没有符合您的搜索条件的记录 所以没有 link 可以点击。
如果您使用 EC.presence_of_element_located
并打印错误,您将清楚地看到发生了什么。
try:
element = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.LINK_TEXT,names))
)
SciName = driver.find_element_by_link_text(names)
except (selenium.common.exceptions.NoSuchElementException,selenium.common.exceptions.TimeoutException) as e:
print(e)
您还需要导入以下内容:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
运行 没有 try/except 的代码每次都会失败。
我正在尝试获取社区类型列表并从网站检索信息。但是,try 语句会出现问题。我需要包含 try 语句,因为列表中有可能找不到的社区类型。当我 运行 没有 try 语句的代码时,代码有效。但是当包含时不起作用并且总是 returns 例外。
import selenium ## web retrieval
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains ## needed
comm = ['Colorado Plateau Mixed Low Sagebrush Shrubland','Nowhere Land']
for names in comm:
driver = webdriver.Firefox()
driver.get("http://explorer.natureserve.org/servlet/NatureServe?init=Ecol")
SciName = driver.find_element_by_name('nameSpec')
SciName.send_keys(names)
checkbox = driver.find_element_by_name('noPunct')
checkbox.click() ### unselect box
SciName.submit() ## this submits it
try: SciName = driver.find_element_by_link_text(names) #
except selenium.common.exceptions.NoSuchElementException:
print names
print "Exception found"
driver.quit()
continue
SciName.click() ## enter this bad boy
print "I made it"
driver.quit() ### close the open window
print names
关于为什么会发生这种情况有什么想法吗?我在另一个网页上使用了相同的代码并且工作正常。
打印错误e
returns:
Message: Unable to locate element: {"method":"link text","selector":"Colorado Plateau Mixed Low Sagebrush Shrubland"}`
和
Message: Unable to locate element: {"method":"link text","selector":"Nowhere Land"}
在您尝试查找可以通过添加 time.sleep:
来验证的元素之前,页面尚未加载SciName.submit() ## this submits it
time.sleep(2)
您会看到第一个成功,但有些仍然失败,因为您得到 没有符合您的搜索条件的记录 所以没有 link 可以点击。
如果您使用 EC.presence_of_element_located
并打印错误,您将清楚地看到发生了什么。
try:
element = WebDriverWait(driver, 3).until(
EC.presence_of_element_located((By.LINK_TEXT,names))
)
SciName = driver.find_element_by_link_text(names)
except (selenium.common.exceptions.NoSuchElementException,selenium.common.exceptions.TimeoutException) as e:
print(e)
您还需要导入以下内容:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
运行 没有 try/except 的代码每次都会失败。