Selenium常见异常解析及解决方案示范

Selenium常见异常解析及解决方案示范,第1张

Selenium常见异常解析及解决方案示范

pycharm中导入selenium报错

现象: pycharm中输入from selenium import webdriver, selenium标红

原因1: pycharm使用的虚拟环境中没有安装selenium,

解决方法: 在pycharm中通过设置或terminal面板重新安装selenium

原因2: 当前项目下有selenium.py,和系统包名冲突导致,

解决方法,重命名这个文件

驱动及本地服务类异常

未找到响应的浏览器驱动

WebDriverException: Message: 'geckodriver' executable needs to be in PATH.

WebDriverException: Message: 'chromedriver' executable needs to be in PATH.

原因: 查找不到对应的浏览器驱动

解决方法: 下载浏览器对应版本的chromedriver或geckodrivergeckodriver

放到脚本当前文件夹下或将路径配置到环境变量中, 或放到Python目录的scripts下(一般情况下Python的scripts目录在环境变量中), 或使用浏览器选项options指定驱动路径

未找到浏览器

WebDriverException: Message Can not connect to the Service chromedriver
org.openqa.selenium.WebDriverException: Failed to connect to binary FirefoxBinary

原因: 在默认路径下未找到Firefox浏览器

解决方法: 重新安装Firefox浏览器

驱动和浏览器不匹配

SessionNotCreatedException: Message: session not created:
this version of ChromeDriver only supports Chrome version 76

原因: 当前使用chromedriver只支持Chrome76版本

解决方法: 查看本地Chrome浏览器的版本, 下载对应的chromedriver

驱动被防火墙拦截

WebDriverException: Message: Can not connect to the Service IEDriverServer.exe

原因: iedriverserver.exe被防火墙拦截

解决方法: 防火墙设置允许

连接不上chromedriver服务

WebDriverException: Message: Can not connect ot the Service chromedriver

原因: 脚本通过127.0.0.1这个ip访问本地chromedriver服务, hosts中未配置 127.0.0.1指向localhost

解决办法: 配置本地hosts, 添加:127.0.0.1 localhost

6.RemoteDriverServerException: 远程服务器异常, 解决方法: 确认webdriver.Remote()中的远程Webdriver服务是否OK

7. ErrorInResponseException: Webdriver服务器响应异常, 解决方法, 根据具体报错信息分析

找不到类异常: 定位/获取属性/切换警告框,frame, 窗口

  1. NoSuchElementException: 找不到元素, 解决方法: 前面加上sleep等待后重试,或换一种定位方式
  2. NoSuchAttributeException: 元素没有这个属性, 解决方法: 确认定位到的元素是否目标元素, 检查属性拼写
  3. NoalertPresentException:没有找到alertd出框, 解决方法: 观察页面,查看是否有d框出现, 加上等待或作为偶现元素处理
  4. NoSuchframeException:没有找到指定的frame或iframe, 解决方法: 查看拼写或切换使用frame的id/name/index/定位到的frame
  5. NoSuchWindowException: 没找到窗口句柄指定的窗口, 解决方法: 查看使用的窗口句柄变量拼写
  6. UnexpectedalertPresentException: 出现了d框而未处理, 解决方法: 切换到警告框并处理, 如果偶现,使用try...except处理偶现d框
  7. InvalidSwitchToTargetException: 切换到指定frame或窗口报错, 解决方法: 查看相应的frame或窗口是否能定位到
  8. UnexpectedTagNameException: 使用Tag Name不合法, 解决方法: 检查拼写或使用css selector/xpath
  9. TimeoutException:查找元素或 *** 作超时, 解决方法, 稍后重试

元素 *** 作异常类: 隐藏/不可 *** 作状态

  1. ElementNotVisibleException:元素不可见异常, selenium不能直接 *** 作隐藏元素, 解决方法: 加上等待, 使用正常步骤使元素显示, 或使用js找到该元素的祖先节点的隐藏属性(通常为styple="display: none"), 移除该属性然后定位 *** 作.
  2. StaleElementReferenceException: 陈旧元素引用异常, 页面刷新或跳转后使用了之前定位到的元素, 解决方法: 重新定位元素并 *** 作
  3. InvalidElementStateException: 元素状态异常 元素只读/不可点击等, 解决方法, 等待或使用js移除元素readonly/disable等限制属性后 *** 作
  4. ElementNotSelectableException:元素不可被选中, 解决方法: 确认原始是否为select标签, 是否禁用
  5. InvalidSelectorException: 使用的定位方法不支持或xpath语法错误, 未返回元素, 解决方法: 检查使用的元素定位器是否拆包, 使用find_element()方法是, 第一个参数为'class name', 'link text', 'particial link text' 'css selector', 空格分开, 非下划线连接, 建议使用By.CLASS_NAME的方式. 使用chrome开发着工具+Ctrl+F搜索验证自己写的xpath语法.
  6. MoveTargetOutOfBoundsException: 使用ActionChains的move方法时移动到的位置不合适

cookie存取相关异常

  1. InvalidcookieDomainException: cookie相应的域名无效
  2. UnableToSetcookieException: 设置cookie异常

IME输入法引擎异常

  1. ImeNotAvailableException: 服务器不支持输入法
  2. ImeActivationFailedException: 输入法激活异常

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://www.outofmemory.cn/zaji/3228012.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-04
下一篇 2022-10-04

发表评论

登录后才能评论

评论列表(0条)

保存