搜索
简帛阁>技术文章>selenium 2.0 'NoneType' object has no attribute 'send_keys'

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的上层,重新获取了一下窗口的句柄,然后在操作。这样就解决了报错的问题。

先把出错的代码贴出来all_handlesactionwindow_handlesforhandleinall_handles:ifhandle!orginal_page:actionswitch_t
运行Python连接mysql数据,查询表格中的数据时报了如下错误AttributeError:'NoneType'objecthasnoattribute'execute'的解决方案pythonge
Python如何解决’NoneType’objecthasnoattribute'…'的问题用Python+selenium和Beautifulsoup爬取MOCC中国大学慕课网上某网上课程的课堂评
今天看python爬虫的视频,然后按照视频上的内容编写代码,运行时出现了AttributeError:'NoneType'objecthasnoattribute'children'错误提示,看了好久
Python3网络编程在构造流媒体或其他’ContentType’类型响应接口时发生如下报错信息:Traceback(mostrecentcalllast):File/usr/lib/python36
环境ubuntu1804python3错误信息Traceback(mostrecentcalllast):File/home/w/local/lib/python36/sitepackages/mat
在写python脚本时遇到AttributeError:'NoneType'objecthasnoattribute'append'a[]b[1,2,3,4]aaappend(b)执行一次后发现a的类
pythonpip'nonetype'objecthasnoattribute'bytes'更新pipforWindows:pythonmpipinstallUpipforLinux:pipinsta
总之就是很蠢的问题,作者本身的代码路径有问题,首先数据集路径必须按照官网设置对,注意大小写:其次我还是报错了,仔细一看:好家伙,路径少了个cityscapes,所以直接用绝对路径,在yaml和city
Python在操作Mysql数据的时候,报了两个错误,一个是AttributeError:'DBHelper'objecthasnoattribute'cursor';另一个是%dformat:anu