try语句在Python 2.7中不返回值

用户名

我正在尝试获取社区类型的列表,并从网站上检索信息。但是,try语句存在问题。我需要包括try语句,因为列表中可能找不到社区类型。当我在不使用try语句的情况下运行代码时,代码将起作用。但是,如果包含不起作用,则始终返回异常。

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返回:

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)

您将看到第一个成功,但是由于您没有得到符合您的搜索条件的记录,因此没有链接,因此仍然有些失败

如果使用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的情况下运行代码都会失败。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

try语句在Python 2.7中不返回值

来自分类Dev

python中的try/except语句

来自分类Dev

在Python中,我的函数不返回值

来自分类Dev

Firebase不返回值(角度2)

来自分类Dev

Python不返回值但显示值

来自分类Dev

语句executeBatch方法返回值2的数组

来自分类Dev

在Python中返回值

来自分类Dev

Python中with语句的等效try语句是什么?

来自分类Dev

ruby中的require语句的返回值

来自分类Dev

r中if语句的返回值

来自分类Dev

python`with .. as ..`语句和多个返回值

来自分类Dev

python if语句返回值错误

来自分类Dev

在函数之外更改值而不返回值-python

来自分类Dev

从Python中的回调返回值

来自分类Dev

在python中打印函数的返回值?

来自分类Dev

从python中的修饰函数返回值

来自分类Dev

python中的递归和返回值

来自分类Dev

在Python for Loop中返回值

来自分类Dev

Python 中递归函数的返回值

来自分类Dev

Python ctypes c_float.value不返回值

来自分类Dev

使用特殊字符时python不返回值

来自分类Dev

如何在Python的IF语句中从多个返回值中检查变量之一的值

来自分类Dev

如何改善Python中try语句的重复使用?

来自分类Dev

使用defaultdict替换python中的try和/或if语句

来自分类Dev

在Python的Try and Except中的“ if”语句中使用“或”

来自分类Dev

在 java 中使用返回值在 try 中混淆执行

来自分类Dev

无法在mysql中存储执行语句的返回值

来自分类Dev

IIF语句未在SSRS中返回值

来自分类Dev

尝试从switch语句-JS-中的函数返回值