Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 嵌套循环AttributeError:_退出__(Seleniu脚本)打开文件时_Python_Selenium_Nested Loops - Fatal编程技术网

Python 嵌套循环AttributeError:_退出__(Seleniu脚本)打开文件时

Python 嵌套循环AttributeError:_退出__(Seleniu脚本)打开文件时,python,selenium,nested-loops,Python,Selenium,Nested Loops,我试图在一个循环中做一个循环,以便在send_键的消息框中传递一个变量。当我打开第二个文件“test2.txt”时,我似乎有一个错误,该文件包含我想要传递给变量的信息。我对python非常陌生,我不完全确定它的含义,也无法通过搜索来解决它 如果您能帮我把这件事做好,我们将不胜感激 代码片段: with open('test2.txt').read() as lp: current = 1 for line in l

我试图在一个循环中做一个循环,以便在send_键的消息框中传递一个变量。当我打开第二个文件“test2.txt”时,我似乎有一个错误,该文件包含我想要传递给变量的信息。我对python非常陌生,我不完全确定它的含义,也无法通过搜索来解决它

如果您能帮我把这件事做好,我们将不胜感激

代码片段:

 with open('test2.txt').read() as lp:
                    current = 1
                    for line in lp:
                        field_box.send_keys("""ID:""", lp)
                        driver.find_element_by_css_selector('.form-buttons').submit()
                        time.sleep(2)
import time
from selenium import webdriver

def doAction():
    driver = webdriver.Chrome ('/Users/xxx/chromedriver') # Set correct path for chromedriver: https://sites.google.com/a/chromium.org/chromedriver/downloads
    driver.get('https://www.site.com.au/login'); # Set which region you're working with
    time.sleep(1)
    search_box = driver.find_element_by_name('username')
    search_box.send_keys('xxx') # Set your e-mail/username
    search_box = driver.find_element_by_name('password')
    search_box.send_keys('xxx') # Set your password
    driver.find_element_by_css_selector('.btn.btn-primary.btn-md.btn-cm.pull-right').click()
    time.sleep(2)
    with open('test.txt') as fp:
        print '============================================'
        print 'Starting to disable entities................'
        print '============================================'
        current = 1
        for line in fp:
            url = 'https://www.site.com.au/admin/entities/%s' % line
            driver.get(url);
            time.sleep(3)
            driver.find_element_by_css_selector("input[type='radio'][value='false']").click()
            field_box_clear = driver.find_element_by_name('disabledReason').clear()
            field_box = driver.find_element_by_name('disabledReason')
            with open('test2.txt').read() as lp:
                current = 1
                for line in lp:
                    field_box.send_keys("""ID:""", lp)
                    driver.find_element_by_css_selector('.form-buttons').submit()
                    time.sleep(2)

            print "Doing",current,"Of",file_len('test.txt')
            current += 1
            print '- Disabled entity: %s' % line
        print '============================================'
        print 'Completed disabling entities................'
        print '============================================'

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

if __name__ == "__main__":
    doAction()
完整代码:

 with open('test2.txt').read() as lp:
                    current = 1
                    for line in lp:
                        field_box.send_keys("""ID:""", lp)
                        driver.find_element_by_css_selector('.form-buttons').submit()
                        time.sleep(2)
import time
from selenium import webdriver

def doAction():
    driver = webdriver.Chrome ('/Users/xxx/chromedriver') # Set correct path for chromedriver: https://sites.google.com/a/chromium.org/chromedriver/downloads
    driver.get('https://www.site.com.au/login'); # Set which region you're working with
    time.sleep(1)
    search_box = driver.find_element_by_name('username')
    search_box.send_keys('xxx') # Set your e-mail/username
    search_box = driver.find_element_by_name('password')
    search_box.send_keys('xxx') # Set your password
    driver.find_element_by_css_selector('.btn.btn-primary.btn-md.btn-cm.pull-right').click()
    time.sleep(2)
    with open('test.txt') as fp:
        print '============================================'
        print 'Starting to disable entities................'
        print '============================================'
        current = 1
        for line in fp:
            url = 'https://www.site.com.au/admin/entities/%s' % line
            driver.get(url);
            time.sleep(3)
            driver.find_element_by_css_selector("input[type='radio'][value='false']").click()
            field_box_clear = driver.find_element_by_name('disabledReason').clear()
            field_box = driver.find_element_by_name('disabledReason')
            with open('test2.txt').read() as lp:
                current = 1
                for line in lp:
                    field_box.send_keys("""ID:""", lp)
                    driver.find_element_by_css_selector('.form-buttons').submit()
                    time.sleep(2)

            print "Doing",current,"Of",file_len('test.txt')
            current += 1
            print '- Disabled entity: %s' % line
        print '============================================'
        print 'Completed disabling entities................'
        print '============================================'

def file_len(fname):
    with open(fname) as f:
        for i, l in enumerate(f):
            pass
    return i + 1

if __name__ == "__main__":
    doAction()

问题是您试图将
open('test2.txt').read()包装为
with

为了实现上下文管理器,我们定义了一个包含 输入和退出方法

open('test2.txt').read()的结果不实现这一点。而不是,您应该将
open('test2.txt')
(自动关闭文件)