前言:这些bug是我在写一个功能型爬虫(京东商品秒杀)是产生的和想到的
发生异常: WebDriverException Message: Service chromedriver.exe unexpectedly exited. Status code was: 1
发生异常: WebDriverException Message:
Service chromedriver.exe unexpectedly exited. Status code was: 1
发生原因:你用的语法和你用driver不匹配,如
driver = webdriver.Firefox(executable_path = 'chromedriver.exe')
#错误类型:用着Firefox的语法调用的却是chrome的驱动(driver)
#解决:
driver = webdriver.Chrome(executable_path = 'chromedriver.exe')
#或者直接用Firefox的driver
driver = webdriver.Firefox(executable_path = 'geckodriver.exe')
当然还会有驱动的版本和浏览器的版本不匹配的,由于我是直接安装成功的,这里仅仅是介绍
错误:selenium.common.exceptions.SessionNotCreatedException: Message: session not created: This version of ChromeDriver only supports Chrome version 89
Current browser version is 91.0.4472.101 with binary path C:\Users\Administrator\AppData\Local\Google\Chrome\Application\chrome.exe
像这种错误是因为浏览器的版本和驱动的版本不匹配导致的,解决的方法也很简单,重新下载相对应的驱动就可以了,一定要配置环境变量啊,不然容易出bug(下载的驱动只需要前面的三位数即主体版号相同即可)。下载地址:
镜像下载地址:https://registry.npmmirror.com/binary.html?path=chromedriver/
详细做法(参考链接):chrome和驱动不匹配的解决方法
基本错误:Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element
Xpath元素无法定位 NoSuchElementException: Message: no such element: Unable to locate element
产生原因:元素定位不成功,可能的bug有两种产生方式,
一:是你代码确实是写错了,Xpath定位定错了,面对这种情况,你只能自己再重新进行定位操作。
二:是页面元素还没有加载完,爬虫是网络请求,网络不好的时候爬虫的一些没有错的代码就产生了错误,xpath靠的是元素,而网不好的时候元素加载不完爬虫就执行下一步命令就会产生错误,对于这种问题的解决方式反而比较简单,因为我们只需要加一个sleep()来限制一下就可以了,如
driver.find_element_by_xpath(
'//*[@id="cart-body"]/div[2]/div[5]/div/div[2]/div/div/div/div[2]/div[2]/div/div[1]/a').click()
#sleep的时间为10
sleep(10)
#sleep过后执行
driver.find_element_by_xpath('//*[@id="order-submit"]').click()
XPath定位参考链接:XPath的思考
其余异常集合:
1.异常:TypeError: 'module' object is not callable
driver = webdriver.chrome()
#解决:浏览器首字母大写
driver = webdriver.Chrome()
2.错误:“chromedriver” executable needs to be in path
#原因1:没有 Chrome 驱动
#解决:需下载驱动添加到环境变量
#原因2:在已经安装的情况
#解决:在代码中添加路径,指明路径
指明路径还报错,检查下自己的驱动版本。
如果添加路径,不报错,证明环境变量有问题
3.事件错误: ElementClick
原因:元素被其他控件遮挡了。
解决:
用 js 去点击。
ele = driver.find_element_by_id(表达式)
driver.execute_script("argument[0].click;", ele)
用鼠标事件去点击。
ele = driver.find_element_by_id(表达式)
webdriver.ActionChains(driver).move_to_element(ele).click(ele).perform()
元素被遮挡了,点击一下旁边的不会产生事件的元素,取消遮挡
4.异常:WebDriverException:Message:Can not connect to the Service chromedriver.exe
WebDriverException:Message:Can not connect to the Service chromedriver.exe
原因:driver驱动被防火墙拦截,解决的办法是允许通过防火墙。
5.异常:WebDriverException:Message:Can not connect to the Service chromedriver
#原因:代码通过 127.0.0.1 这个ip 访问 chromedriver 服务,
#hosts文件没有配置127.0.0.1指向localhost。
#解决:配置本地hosts文件,添加 127.0.0.1 localhost。
6.各种找不到:
NoSuchElementException:
找不到元素。
NoSuchAttributeException:
元素没有这个属性,确认定位到的元素是否具备目标属性,或检查一下单词拼写。
NoAlertPresentException:
没有找到 alert 弹窗,观察页面,查看是否有弹窗出现,或加上等待。
NoSuchFrameException:
没有找到内嵌网页,检查元素定位、或者单词拼写。
NoSuchWindowException:
没找到窗口,窗口是不是被提前关闭了,或者检查单词拼写、列表索引、或判断条件。
TimeOutException:
在显示等待,或隐式等待中,查找元素超时,也就是找不到元素。
7.各种元素操作错误:
ElementNotVisibleException:元素不可见异常(原因:selenium不能操作隐藏元素)
可能元素在某操作后需要反应一下,才会从隐藏状态变为可见。
解决:可用 sleep 等待。
可能元素需要某些步骤之后,才会出现。
解决:去执行这些步骤。
如果一定要操作隐藏元素,可用 js 语法移出元素的不可见属性。
(style=“display:none”)
document.querySelector("li[class='b_ans b_top b_topborder']").removeAttribute("h")
表达式写 css 表达式定位
StaleElementException: 陈旧的元素引用异常
原因:页面刷新了,或页面跳转之后,使用了之前定位的元素。
解决:重新定位元素并操作
InvalidElementStateException:元素状态异常
原因:元素只读、不可点击等。
解决:等待或使用js移出影响属性。
ElementNotSelectableException:元素不可被选中
确认标签是 select
MoveTargetOutOfBoundsException:鼠标事件移动的位置不合适
解决:通常结合页面当前状态,换个位置即可。