我试图从python中的seroium在chromedriver中的标记中获取文本。我的代码正确运行,但是当没有应该得到的标记时,脚本会发出错误并停止。错误:Python - 为引发异常处理exception_class(消息,屏幕,堆栈跟踪)
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//span[@class="section-facts-description-text"]"}
(Session info: chrome=56.0.2924.87)
(Driver info: chromedriver=2.27.440174(e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)
我想使异常,所以当硒找不到标签,它会打印一条消息,而不是停止脚本。我的代码:
import os
import sys
from selenium import webdriver
def findLocation(location):
browser = webdriver.Chrome()
print "please wait, I'll show you where "+location+" is"
browser.get("https://www.google.co.id/maps/place/"+location+"/")
try:
elem = browser.find_element_by_xpath('//span[@class="section-facts-description-text"]')
desc = elem.text
print str(desc)
except:
print "There's no description about this location"
我不知道我应该用什么异常来处理这个错误。如果我只使用except
它会打印“没有这个位置描述”太甚至有跨度标签