小编DaN*_*NuN的帖子

如果未找到元素,则毫无例外地检查元素

我正在尝试探测页面是否存在特定元素,因为它会改变我的进程中的条件。我使用 a.find_element_by_xpath来查找该元素,它工作正常,但如果在没有该元素的页面上执行该搜索,我会收到异常并停止进程。

我是硒的新手,查看了文档,但我只是找不到我要找的东西。

任何指导表示赞赏。

我的处理程序:

try:
    browser.find_element_by_xpath('//*[@id="message"]')
except NoSuchElementException as e:
    print('No Such Element has occurred')
    pass
Run Code Online (Sandbox Code Playgroud)

这是跟踪:

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pydev\pydev_run_in_console.py", line 53, in run_file
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.3.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 187, in <module>
    advertise()
  File "C:/Users/DWL/Desktop/comment_bot/comment_bot.py", line 102, in advertise
    advert = [popper(adv_vc, add_vc, ad_comment) for x in range(len(adv_vc))]
  File …
Run Code Online (Sandbox Code Playgroud)

python selenium python-3.6

4
推荐指数
1
解决办法
1万
查看次数

标签 统计

python ×1

python-3.6 ×1

selenium ×1