Selenium Python跳过NoSuchElementException错误

时间:2018-02-27 00:42:52

标签: python selenium selenium-webdriver selenium-chromedriver

我的老师正在让我做一个页面,我应该点击一个术语并将其与其定义相匹配。屏幕上有30个单词,一次只能有6个(其匹配定义,总共1个)。我有一个包含所有单词和定义的数组,因此python可以为我匹配,但是,如果数组中的第一个单词没有显示,则代码不起作用。我该如何绕过这个错误?这是我的代码:

term = ["fug,fuge", "duc,duce,duct", "ortho", "morph", "mot,mob,mov,cine,kine", "ible, able", "flect, flex", "flu", "cide,sec,seg", "miso", "mort", "ology", "port", "ject,jet", "miss,mitt", "viv", "vert, volv, rota", "naut", "act", "klept", "frac,frag,rupt", "simil", "struct", "tens", "therm", "luc,lum,photo", "foli,phyll", "tox", "bene,bon", "agr"]
definition = ["drive away,flee", "to lead", "right,straight,correct", "change", "to move", "able to be", "to bend", "to flow", "to kill,cut", "hate", "to die", "science,study of", "carry", "to throw", "to send", "to live", "to turn", "to sail", "to do, make", "to steal", "to break", "to resemble", "to build", "to stretch", "heat", "light", "poison", "good", "field"]

x = 0

while x < 30:
    t = driver.find_element_by_xpath("//div[contains(text(), '"+term[x]+"')]")
    t.click()
    d = driver.find_element_by_xpath("//div[contains(text(), '" + definition[x] + "')]")
    d.click()

    x=x+1

以下是我遇到的错误:

Traceback (most recent call last):
  File "/Users/username/Desktop/q_hack.py", line 16, in <module>
    t = driver.find_element_by_xpath("//div[contains(text(), '"+term[x]+"')]")
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 354, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 832, in find_element
    'value': value})['value']
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 297, in execute
    self.error_handler.check_response(response)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 194, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//div[contains(text(), 'fug,fuge')]"}

发生了什么事?为什么会出现错误?我该如何解决?以下是该页面的屏幕截图:

Image

正如您所看到的,并非所有30个单词都在那里。提前致谢

1 个答案:

答案 0 :(得分:1)

您可以使用try-except语句。但是,使用一般的除处理程序并不是一个好主意。

term = ["fug,fuge", "duc,duce,duct", "ortho", "morph", "mot,mob,mov,cine,kine", "ible, able", "flect, flex", "flu", "cide,sec,seg", "miso", "mort", "ology", "port", "ject,jet", "miss,mitt", "viv", "vert, volv, rota", "naut", "act", "klept", "frac,frag,rupt", "simil", "struct", "tens", "therm", "luc,lum,photo", "foli,phyll", "tox", "bene,bon", "agr"]
definition = ["drive away,flee", "to lead", "right,straight,correct", "change", "to move", "able to be", "to bend", "to flow", "to kill,cut", "hate", "to die", "science,study of", "carry", "to throw", "to send", "to live", "to turn", "to sail", "to do, make", "to steal", "to break", "to resemble", "to build", "to stretch", "heat", "light", "poison", "good", "field"]

x = 0

while x < 30:
    try:
        t = driver.find_element_by_xpath("//div[contains(text(), '"+term[x]+"')]")
        t.click()
        d = driver.find_element_by_xpath("//div[contains(text(), '" + definition[x] + "')]")
        d.click()
    except selenium.common.exceptions.NoSuchElementException:
        pass

    x=x+1