Skip to main content
 首页 » 编程设计

python之Selenium Python 跳过 NoSuchElementException 错误

2025年01月19日9虾米姐

有一个页面是我的老师让我做的,我应该点击一个术语并将其与其定义相匹配。有 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 个词都在上面。提前致谢

请您参考如下方法:

您可以使用 try-except 语句。不过,使用通用的 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