webdriver+python录制百度首页,定位不到问题

最终代码如下:

#coding=utf-8

from selenium import webdriver

browser=webdriver.Ie()
browser.get("http://www.baidu.com")


browser.switch_to_frame("bd")
browser.find_element_by_xpath(".//input[@id='kw']").send_keys("selenium")
browser.find_element_by_id("su").click()


browser.quit()

昨天一开始是用火狐执行,火狐版本定位语句是:browser.find_element_by_id("kw").send_keys("selenium")执行成功了。以为脚本OK了,而后换成Ie测试,但是提示:WebDriverException: Message: Unexpected error launching Internet Explorer. Protected Mode settings are not the same for all zones. Enable Protected Mode must be set to the same value (enabled or disabled) for all zones.当时去只去掉了Internet的保护模式,再运行还是报这错。把换回来firefox,发现居然提示:Message:unable to find element。

今天换成find_element_by_xpath(".//*[@id='kw']")定位继续调试,发现错误仍是如此。仔细看了下百度首页页面元素,不存在iframe等包裹input,但是错误还是原样。maybe是因为这个点https://www.baidu.com/?tn=monline_3_dg查看的。于是在群里问下了,群友说把*换成input,换IE测试,保护模式需要把Internet、本地Intranet、受限站点等的都去掉勾选。照做之后发现执行成功了。


以为到此结束了,等等,又一轮报错开始了。继续换成火狐运行,提示:

NoSuchElementException: Message: Unable to locate element: {"method":"xpath","selector":".//input[@id='kw']"}
Stacktrace:
    at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/admini~1/appdata/local/temp/tmpc18dgn/extensions/fxdriver@googlecode.com/components/driver-component.js:10659)
    at FirefoxDriver.prototype.findElement (file:///c:/users/admini~1/appdata/local/temp/tmpc18dgn/extensions/fxdriver@googlecode.com/components/driver-component.js:10668)
    at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/admini~1/appdata/local/temp/tmpc18dgn/extensions/fxdriver@googlecode.com/components/command-processor.js:12534)
    at DelayedCommand.prototype.executeInternal_ (file:///c:/users/admini~1/appdata/local/temp/tmpc18dgn/extensions/fxdriver@googlecode.com/components/command-processor.js:12539)
    at DelayedCommand.prototype.execute/< (file:///c:/users/admini~1/appdata/local/temp/tmpc18dgn/extensions/fxdriver@googlecode.com/components/command-processor.js:12481)

只好又换回IE,运行,不妙了,现象循环了。IE下运行也报错了,也是无法定位。崩溃了……然后在想从这个文件目录来看推断是不是selenium本身的错误,比如我引入的模块不够,不支持运行了?网上兜兜转转找了一圈,没有类似的问题。于是乎返回来检查百度的页面发现文档又出来了一个frameset。呵呵哒。


于是乎,转而定位frame,网上有找点有用的回答,说是frameset没有id、name的时候可以用序号定位。然后加入了switchTo.frame(1),始终提示:AttributeError: 'WebDriver' object has no attribute 'switchTo',累了,发现好像还是得要找是否少了引入模块。找了找,有人问类似不支持属性的问题,说也有可能是属性方法写错了。最终把语句写成browser.switch_to_frame("bd"),这个name="bd"是通过火狐查看页面元素得来的

最终成了上述代码。IE执行成功,火狐仍是无法定位。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值