Cannot maintain focus on element in Selenium Python driver

Viewed 1458

I am trying to control the web by python to run a script and download the corresponding csv file.

Here is how the web page looks like with a dashboard menu to click the "Search" other button. Once clicked on Search button it shows a Search text box where one can enter a code and press enter to run.

enter image description here

Now I need to find the element of this Search box. From Inspect in Chrome, looks like below:

enter image description here

enter image description here

So I used the following code. I also used Actions to keep the focus on search box before I copy the code from a text file and send it to that search box.

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()

However it throws an error of focus not on element.

I am not sure if I got the right element selector for Search from the attached html file, or it is just some focus issue. I did use Actions class there to get the focus.

I want the code to read the text in the txt file and send it to the search box and press enter to run it.

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

EDIT: Extra html details for selector

enter image description here

Edit 2:

enter image description here

Edit 3:

So I am able to get the element for Search and it is able to copy the code from a txt file and enter in search box but I see it is not able to copy the whole code correctly hence gives an error. Pls see attached full code and how much got copied.

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:

The code read from the txt file is also reading \n. So the string has \n in it and I guess that might be causing issues when sent to the WebDriver to run in the search box. Possible to read the code as it is in above edit?

'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 Answers

You should send keys to input field, but not to parent div. Try below instead:

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

or

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

Also note that most likely you won't need to use Actions

content_box=driver.find_element_by_class_name("ace_content")

this code will result in content_box being a "div" element. you can't send keys to a div element. inspect that div to find a "textarea" or "input" element, and set that to your content_box.

On top of @Andersson's answer (which you should accept btw, he did solve your problem ;) let me help you with stripping the \n from the source text. This code:

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

, the read() method, returns the raw value of the file, with the EOL (end-of-line) characters intact. This though:

code_file = f.read.splitlines()

, will return it (in code_file) as a list of strings, each list member a line in the file. Now the question is - what to replace the EOL chars with? I'm not familiar with the language that's in it, so it's up to you to decide.
Say it is a semicolon, ;, this is how to transform the list back into a string:

code_file = ';'.join(code_file)

This will concatenate all list members in a single string, using that character as delimiter. Naturally, you just replace the char with whatever is applicable:

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

So the final form is:

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