3

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'

1
  • Its a private login so can't share the login. But the webpage shows that in attached screen. I am not sure if I got the right element. That is why I shared the corresponding css. It didn't give me error with respect to element. So I assumed I got the right element there for Search box. Commented Jan 11, 2019 at 22:18

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

12 Comments

added another edit with extra css file details. Not sure I see any Input field there. Its all div
This doesn't work. Says compound class names not acceptable. So I just used "div.ace_content" and it says selector not found
@Baktaawar , note that I've used find_element_by_css_selector, but not find_element_by_class_name
@Baktaawar , also try driver.find_element_by_class_name('ace_text-input').send_keys(code_file, Keys.ENTER)
by selector doesn/t work. But by class_name worked as I just found there is a class above that which has that name. Check edit and image. So now I am able to get the text file code pasted in the search box but it doesn't run. And gives an error
|
0
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.

1 Comment

I am not sure I am seeing any input there. I have added another screenshot to show what else is there in the css file.
0

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)

10 Comments

Hey thanks for suggestion. I know about joining lists of strings. But I think it's not the \n issue then. Because if I replace \n with space it's still not coping the whole text in the text box. So I think it's an issue with copying or sending text data to a text box using python selenium? Is there a limit to what one can send? I even copied this whole text as a string in a variable instead of reading from a file and sent that using send_keys, but it is coping few characters but not all.
No, I don't think it's a restriction in Selenium how much data it can send (I'm quite positive there's no such); it's much more probable there's an UI (js) validation or length restriction that's stopping you.
Any idea how I can figure it out? It's not reading the whole text continuously and picks up characters from between and then stops. I am able to send small texts (like user name and password in login fields) correctly
Try it by hand, copy the text and paste, see how and where it breaks.
Oh no. By hand it works. That is how I have been doing all this time. I want to automate it using this Selenium web driver. So there is no issue with this text at all as if I copy it and paste in the search box and press enter, it runs.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.