无法保持对Selenium Python驱动程序中元素的关注

时间:2019-01-11 21:57:18

标签: python selenium

我试图通过python控制网络以运行脚本并下载相应的csv文件。

这是使用仪表板菜单单击“搜索”其他按钮的网页外观。单击“搜索”按钮后,将显示一个“搜索”文本框,您可以在其中输入代码并按Enter键运行。

enter image description here

现在,我需要找到此搜索框的元素。从Chrome中的Inspect中,如下所示:

enter image description here

enter image description here

因此,我使用了以下代码。在从文本文件复制代码并将其发送到该搜索框之前,我还使用了动作来使焦点集中在搜索框上。

def run_code():

""" Function to copy the code in Search and run it

"""

search_button=driver.find_element_by_link_text("Search")
search_button.click()

time.sleep(2)

with open('data_download_code.txt', 'r') as f:
    code_file= f.read()

content_box=driver.find_element_by_class_name("ace_content")

# Getting the focus on the element
actions=ActionChains(driver)
actions.move_to_element(content_box)
actions.click()

content_box.send_keys(code_file,Keys.ENTER)
#content_box.submit()

但是,它将引发焦点错误而不是元素错误。

我不确定是否从所附的html文件中找到了用于搜索的正确元素选择器,否则这只是一些焦点问题。我确实在那里使用了Actions类来获得焦点。

我希望代码读取txt文件中的文本并将其发送到搜索框,然后按Enter键运行它。

WebDriverException: Message: unknown error: cannot focus element
  (Session info: chrome=71.0.3578.98)

编辑:选择器的额外html详细信息

enter image description here

编辑2:

enter image description here

编辑3:

因此,我能够获取Search元素,并且能够从txt文件复制代码并在搜索框中输入内容,但我发现它无法正确复制整个代码,因此会出现错误。请查看随附的完整代码以及复制了多少代码。

sourcetype=perf_log_bizx
(host=pc*bcf* OR host=pc*bsfapi* OR servername=pc*bcf* OR servername=pc*bsfapi*) OR
(host=sc*bcf* OR host=sc*bsfapi* OR servername=sc*bcf* OR servername=sc*bsfapi*) OR
(host=ac*bcf* OR host=ac*bsfapi* OR servername=ac*bcf* OR servername=ac*bsfapi*) OR
NOT "/perfLogServlet" NOT "REQ-\[*" earliest="12/18/2018:08:00:00" latest="12/18/2018:12:00:00" 

   | rex field=_raw "\[(?<client_ip>[\d\.]+)\]\s+\[(?<company_id>[^,]+),(?<company_name>[^,]+),(?<company_schema>[\w\.]+),(?<dbpool>[^,]+),(?<user_id>[^,]+),\S+\]\s+\S+\s+\S+\s+(?<render_time>\d+)\s(?<server_time>\d+)\s(?<end2end_time>\d+)\s+\S+\s\S+\s\[.*\]\s+\d+\-(?<call_id>\d+)\s+(?<module_id>(-|\w+))\s(?<page_name>(-|\w+))\s(?<page_qualifier>(-|\w+))"
    | rex field=_raw "\[\[(?<MemoryAllocated>\d+)\w+\s+(?<CPUTimeTotal>\d+)\w+\s+(?<CPUTimeUser>\d+)\w+\s+(?<CPUTimeSystem>\d+)\w+\s+(?<FileRead>\d+)\w+\s+(?<FileWrite>\d+)\w+\s+(?<NetworkRead>\d+)\w+\s+(?<NetworkWrite>\d+)\w+\s+(?<NotClosedFiles>(\d+|-))\s+(?<NotClosedSockets>(\d+|-))\s+\]\]\s+(?<SQLInvokes>\d+)\s+(?<SQLTimeTotal>\d+)"

    | eval company_id = ifnull(CMID, company_id)
    | eval dbpool = ifnull(DPN, dbpool)
    | eval company_schema =ifnull(SN, company_schema)
    | eval user_id = ifnull(UID, user_id)

    | eval module_id = ifnull(MID, module_id)
    | eval page_name = ifnull(PID, page_name)
    | eval page_qualifier = ifnull(PQ, page_qualifier)

    | rex field=CAID "\d+\-(?<CAID>\d+)"
    | eval call_id = ifnull(CAID, call_id)

    | eval render_time = ifnull(RDT, render_time)
    | eval server_time = ifnull(SVT, server_time)
    | eval end2end_time = ifnull(EET, end2end_time)
    | eval MemoryAllocated = ifnull(MEM, MemoryAllocated)
    | eval CPUTimeTotal = ifnull(CPU, CPUTimeTotal)
    | eval CPUTimeUser = ifnull(UCPU, CPUTimeUser)
    | eval CPUTimeSystem = ifnull(SCPU, CPUTimeSystem)
    | eval FileRead = ifnull(FRE, FileRead)
    | eval FileWrite = ifnull(FWR, FileWrite)
    | eval NetworkRead = ifnull(NRE, NetworkRead)
    | eval NetworkWrite = ifnull(NWR, NetworkWrite)
    | eval NotClosedFiles = ifnull(0, NotClosedFiles)
    | eval NotClosedSockets = ifnull(0, NotClosedSockets)
    | eval SQLInvokes = ifnull(SQLC, SQLInvokes)
    | eval SQLTimeTotal = ifnull(SQLT, SQLTimeTotal)

    | eval request_type = if(call_id=0,"Root", "Subaction")

| search call_id = 0 AND page_name!="NONE"

    | eval full_page_name = module_id + "-" + page_name + "-" + page_qualifier + " [" + request_type + "]"
    | eval has_open_sockets = if ( ifnull(NotClosedSockets,0) > 0, 1, 0)
    | eval has_open_files = if ( ifnull(NotClosedFiles,0) > 0, 1, 0)
    | eval time = strftime( _time, "%Y-%m-%d %H:%M:%S" )
    | eval server = ifnull(servername, host)
    | rex field=server "\w(?<dc>\d+)\w"
    | eval dc_name = "DC" + tostring(dc)
    | eval server_type = if (substr(server, 1, 2) = "pc", "PROD", if (substr(server, 1, 2) = "sc", "PREVIEW", if (substr(server, 1, 2) = "ac", "QA", "OTHER") ) )
    | eval dc_company_user =  dc + "|" + company_id + "|" + sha256( user_id )

| table
     time,
     dc_name,
     server_type,
     dbpool,
     company_id,
     full_page_name,
     dc_company_user,
     server_time,
     end2end_time,
     SQLInvokes,
     SQLTimeTotal,
     MemoryAllocated[![enter image description here][6]][6]

enter image description here

Edit4:

从txt文件读取的代码也为\ n。因此该字符串中包含\ n,我想这可能会导致在发送到WebDriver以便在搜索框中运行时引起问题。可以读取上面编辑中的代码吗?

'sourcetype=perf_log_bizx\n(host=pc*bcf* OR host=pc*bsfapi* OR servername=pc*bcf* OR servername=pc*bsfapi*) OR\n(host=sc*bcf* OR host=sc*bsfapi* OR servername=sc*bcf* OR servername=sc*bsfapi*) OR\n(host=ac*bcf* OR host=ac*bsfapi* OR servername=ac*bcf* OR servername=ac*bsfapi*) OR\nNOT "/perfLogServlet" NOT "REQ-\\[*" earliest="12/18/2018:08:00:00" latest="12/18/2018:12:00:00" \n \n | rex field=_raw "\\[(?<client_ip>[\\d\\.]+)\\]\\s+\\[(?<company_id>[^,]+),(?<company_name>[^,]+),(?<company_schema>[\\w\\.]+),(?<dbpool>[^,]+),(?<user_id>[^,]+),\\S+\\]\\s+\\S+\\s+\\S+\\s+(?<render_time>\\d+)\\s(?<server_time>\\d+)\\s(?<end2end_time>\\d+)\\s+\\S+\\s\\S+\\s\\[.*\\]\\s+\\d+\\-(?<call_id>\\d+)\\s+(?<module_id>(-|\\w+))\\s(?<page_name>(-|\\w+))\\s(?<page_qualifier>(-|\\w+))"\n | rex field=_raw "\\[\\[(?<MemoryAllocated>\\d+)\\w+\\s+(?<CPUTimeTotal>\\d+)\\w+\\s+(?<CPUTimeUser>\\d+)\\w+\\s+(?<CPUTimeSystem>\\d+)\\w+\\s+(?<FileRead>\\d+)\\w+\\s+(?<FileWrite>\\d+)\\w+\\s+(?<NetworkRead>\\d+)\\w+\\s+(?<NetworkWrite>\\d+)\\w+\\s+(?<NotClosedFiles>(\\d+|-))\\s+(?<NotClosedSockets>(\\d+|-))\\s+\\]\\]\\s+(?<SQLInvokes>\\d+)\\s+(?<SQLTimeTotal>\\d+)"\n \n | eval company_id = ifnull(CMID, company_id)\n | eval dbpool = ifnull(DPN, dbpool)\n | eval company_schema =ifnull(SN, company_schema)\n | eval user_id = ifnull(UID, user_id)\n \n | eval module_id = ifnull(MID, module_id)\n | eval page_name = ifnull(PID, page_name)\n | eval page_qualifier = ifnull(PQ, page_qualifier)\n \n | rex field=CAID "\\d+\\-(?<CAID>\\d+)"\n | eval call_id = ifnull(CAID, call_id)\n \n | eval render_time = ifnull(RDT, render_time)\n | eval server_time = ifnull(SVT, server_time)\n | eval end2end_time = ifnull(EET, end2end_time)\n | eval MemoryAllocated = ifnull(MEM, MemoryAllocated)\n | eval CPUTimeTotal = ifnull(CPU, CPUTimeTotal)\n | eval CPUTimeUser = ifnull(UCPU, CPUTimeUser)\n | eval CPUTimeSystem = ifnull(SCPU, CPUTimeSystem)\n | eval FileRead = ifnull(FRE, FileRead)\n | eval FileWrite = ifnull(FWR, FileWrite)\n | eval NetworkRead = ifnull(NRE, NetworkRead)\n | eval NetworkWrite = ifnull(NWR, NetworkWrite)\n | eval NotClosedFiles = ifnull(0, NotClosedFiles)\n | eval NotClosedSockets = ifnull(0, NotClosedSockets)\n | eval SQLInvokes = ifnull(SQLC, SQLInvokes)\n | eval SQLTimeTotal = ifnull(SQLT, SQLTimeTotal)\n \n | eval request_type = if(call_id=0,"Root", "Subaction")\n \n| search call_id = 0 AND page_name!="NONE"\n \n | eval full_page_name = module_id + "-" + page_name + "-" + page_qualifier + " [" + request_type + "]"\n | eval has_open_sockets = if ( ifnull(NotClosedSockets,0) > 0, 1, 0)\n | eval has_open_files = if ( ifnull(NotClosedFiles,0) > 0, 1, 0)\n | eval time = strftime( _time, "%Y-%m-%d %H:%M:%S" )\n | eval server = ifnull(servername, host)\n | rex field=server "\\w(?<dc>\\d+)\\w"\n | eval dc_name = "DC" + tostring(dc)\n | eval server_type = if (substr(server, 1, 2) = "pc", "PROD", if (substr(server, 1, 2) = "sc", "PREVIEW", if (substr(server, 1, 2) = "ac", "QA", "OTHER") ) )\n | eval dc_company_user = dc + "|" + company_id + "|" + sha256( user_id )\n \n| table\n time,\n dc_name,\n server_type,\n dbpool,\n company_id,\n full_page_name,\n dc_company_user,\n server_time,\n end2end_time,\n SQLInvokes,\n SQLTimeTotal,\n MemoryAllocated'

3 个答案:

答案 0 :(得分:2)

您应该将密钥发送到input字段,而不是父密钥div。请尝试以下操作:

content_box = driver.find_element_by_css_selector("div.ace_content input")
content_box.send_keys(code_file, Keys.ENTER)

content_box = driver.find_element_by_class_name('ace_text-input')
content_box.send_keys(code_file, Keys.ENTER)

还请注意,您很可能不需要使用Actions

答案 1 :(得分:0)

content_box=driver.find_element_by_class_name("ace_content")

此代码将导致content_box为“ div”元素。您不能将键发送到div元素。检查该div以查找“ textarea”或“ input”元素,并将其设置为content_box。

答案 2 :(得分:0)

在@Andersson的回答(您应该接受顺便说一句,他确实解决了您的问题;)之外,让我帮助您从源文本中剥离\n。这段代码:

with open('data_download_code.txt', 'r') as f:
    code_file= f.read()

read()方法,返回文件的原始值,并保留EOL(行尾)字符。但是:

code_file = f.read.splitlines()

,将以字符串列表的形式返回(在code_file中),每个列表成员在文件中占一行。现在的问题是-用什么替换EOL字符?我不熟悉其中的语言,因此由您决定。
假设它是一个分号;,这是将列表转换回字符串的方法:

code_file = ';'.join(code_file)

这将使用该字符作为分隔符将所有列表成员连接到一个字符串中。自然地,您只需将char替换为适用的内容:

code_file = ' '.join(code_file)    # a whitespace character
code_file = '\t'.join(code_file)   # a tab
code_file = '\\n'.join(code_file)  # a literal newline
code_file = 'whatever?'.join(code_file)    # you name it

所以最终形式是:

with open('data_download_code.txt', 'r') as f:
   code_file= f.readlines()
   code_file = ';'.join(code_file)