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

来源:互联网 发布:好运来返奖统计软件 编辑:程序博客网 时间:2024/06/11 15:13

先把出错的代码贴出来

all_handles=action.window_handlesfor 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 0
原创粉丝点击