selenium 2.0 'NoneType' object has no attribute 'send_keys'

先把出错的代码贴出来

all_handles=action.window_handles
for handle in all_handles:
    if handle!=orginal_page:
        action.switch_to.window(handle) 
        action.switch_to.frame("frameMu_0")
        for lists in types:
            for pl in product_list:                     
                pl=pl.decode('utf-8')           
                action.find_element_by_id("pName").send_keys(pl)
                action.find_element_by_id("btn2").click()
                try:
                    action.find_element_by_name("pId").click()
                except:
                    action.get_screenshot_as_file("Add_Normal_Product_Error"+date1+"-"+date2+".jpg")
                    with open("Test_Error"+date1+".log",'a+') as ef:
                        print >>ef,"error\t"+date3+"\n没有搜索到相关产品\n"
                    time.sleep(2)
                    action.quit()
                else:
                    action.find_element_by_id("pName").clear()          

                action.switch_to.default_content()
                action.find_element_by_id("button2").click()

action.switch_to.window(orginal_page)
action.switch_to.frame("34")
pro=action.find_element_by_css_selector("#contractTab > div.tabs-panels > div:nth-child(1) > div > iframe")
action.switch_to.frame(pro)
action.find_element_by_css_selector("body > div:nth-child(1) > table > tbody > tr:nth-child(1) > td:nth-child(7) > a").click()
time.sleep(2)

执行上面的代码报错:
‘NoneType’ object has no attribute ‘send_keys’
后经高人指点,才知道在循环里切回了default_content,这样再循环的时候就不再frame里面了。
后来高人说应该把iframe写在循环里。虽然按高人说的做了,但是还是没有达到预期的目标。
后来自己又琢磨了一下,就有了下面的代码:

all_handles=action.window_handles
        for handle in all_handles:
            if handle!=orginal_page:                    
                action.switch_to.window(handle) 
                action.switch_to.frame("frameMu_0")
                for pl in product_list:                     
                    pl=pl.decode('utf-8')       
                    action.find_element_by_id("pName").send_keys(pl)
                    action.find_element_by_id("btn2").click()
                    try:
                        action.find_element_by_name("pId").click()
                    except:
                        action.get_screenshot_as_file("Add_Normal_Product_Error"+date1+"-"+date2+".jpg")
                        with open("Test_Error"+date1+".log",'a+') as ef:
                            print >>ef,"error\t"+date3+"\n没有搜索到相关产品\n"
                        time.sleep(2)
                        action.quit()
                    else:
                        action.find_element_by_id("pName").clear()  

                all_handles=action.window_handles
                for handle in all_handles:
                    if handle!=orginal_page:                                    
                        action.switch_to.window(handle)
                        action.find_element_by_id("button2").click()


action.switch_to.window(orginal_page)
action.switch_to.frame("34")
pro=action.find_element_by_css_selector("#contractTab > div.tabs-panels > div:nth-child(1) > div > iframe")
action.switch_to.frame(pro)
action.find_element_by_css_selector("body > div:nth-child(1) > table > tbody > tr:nth-child(1) > td:nth-child(7) > a").click()
time.sleep(2)

也是按照高人指点的思路,只是稍作改动,不再返回iframe的上层,重新获取了一下窗口的句柄,然后在操作。这样就解决了报错的问题。

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值