0

I have most of the code working for this now im just looking for selenium to get a console.log from Chrome

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("http://192.168.0.3:3333/")
time.sleep(1)

for entry in driver.get_log('browser'):
    print entry
print

The problem im getting is 'webdriver' has no attribute 'get_log()' I have tried using 'driver.ChromeOptions.get_log()' but no attribute ChromeOptions.

im a little lost with this module as i have tried example code with the same results.

  • Are you looking for something like this: http://stackoverflow.com/questions/22087881/how-to-save-and-edit-a-file-using-javascript – Pav Sidhu Jul 11 '15 at 21:35
  • not exactly. I just want to have a button on a html page that when clicked will input the word "hello" into an active notepad document that's being edited. Its basically a hotkey function but made with JavaScript – Scott Paterson Jul 11 '15 at 23:38
  • 1
    For security reasons `javascript` does not have access to what is installed or running on your computer. because you mentioned you want to stick to the windows machine, what you want to do can best be done by developing an `activex`. you'll also have some limitations in it, but I think that's as far as you can go by accessing clients resources. – EhsanT Jul 12 '15 at 01:36
  • this is proving to be a difficult project. from what iv seen Activex does not run on mac or linux. Having cross platform support for this is a must. if the js or other scripting language can access an application of some sort to forward on the strings (like an virtual keyboard application) could that be done ? – Scott Paterson Jul 12 '15 at 02:29
  • 1
    @ScottPaterson, as far as I know, no client-side scripting language is able to do so. just imagine if it was doable, what would hackers do to poor clients computer! – EhsanT Jul 12 '15 at 03:35
  • if you have to have a cross-platform solution, but on all of these platforms you can use only one specific browser, then maybe you can choose only _Mozilla Firefox_ or _Google Chrome_ and then develop an _Extension_ or _Add On_ for them. so the clients have to install that _Extension_ on their machines and maybe that _Extension_ can take care of what you want to achieve. but I have to admit that I am not aware of the technical limitations _Extensions_ have. so maybe you can do some research on this matter. – EhsanT Jul 12 '15 at 03:45
  • Just so im clear im not talking about it being actually web based. just using a HTML style front page. out of all the languages i have used HTML and CSS are some of the easiest tools to design with. Right now im looking into c# as well as its multi platform support looks good and it already has embedded key press functions. The idea was to have JS to output to a seperate application using a more functional language (like c#) which can then control the actual keypressing. – Scott Paterson Jul 12 '15 at 04:52
  • So it would be something like Html button with on click js function outputting to a c# code to press a set of keys. the html would control the style and look while the JS would just send a message to the application as a string value and the application would simulate the buttons, all local based however no internet connection supported. – Scott Paterson Jul 12 '15 at 04:55
  • If I was you, instead of looking into C#, I would look into Python, which already comes with your Raspberry Pi. The syntax is basically like English and you can get what you want up and running sooner, depending on how much you have learnt on C#. There's even a Python framework called Flask which allows you to make webpages like the one you want, so you can edit and create files. If you have not learnt much on C# I would look into this. – Pav Sidhu Jul 12 '15 at 10:57
  • In this case, `html`, `css` and `javascript` are just used to design the web page and send the message, but the real work is done by the code you have to develop in some other language which has to be cross-platform. then I think as @PavSidhu has mentioned you can use `Python` or even as you said you can use `C#`. SO is not a good place to get opinions from users, since we are here to help others with their problems in their codes. you can do some research on the net for choosing the best language which suits you. you focus should not be on `html` and `js`. – EhsanT Jul 12 '15 at 16:29
  • Ok so got python SendKeys working and im fairly familiar with Flask, how can i get a js function to output to python using Flask ? – Scott Paterson Jul 13 '15 at 01:58
  • I think you can use `JSON` data structure to send your data from `javascript` and then use `console.log` to send them to console. then you can use `python` to read the console log and interpret them. I'm not familiar with `python` coding, but found [this link](http://stackoverflow.com/questions/20907180/getting-console-log-output-from-chrome-with-selenium-python-api-bindings) that may help you. – EhsanT Jul 13 '15 at 15:14
  • Thanks for the suggestion. I have been working for a few hours trying this suggestion and i believe it will work. however im struggling to make it do so. I have updated my question to reflect new changes – Scott Paterson Jul 14 '15 at 19:34
  • Modified question again for new info when using selenium – Scott Paterson Jul 16 '15 at 22:52

1 Answers1

0

I finally got it working. Rather than using console.log i used the value of a div and use a onclick js function so set value to "key1", then another js function to clear the value on a 250ms delay. (looking for a better way of doing this.)

function jsfun1() {
document.getElementById('keyvalue').value = 'Key Press 1';
}

The selenium code basically checks the value of the div and prints it ever 0.1 seconds.

d = DesiredCapabilities.CHROME
d['loggingPrefs'] = { 'browser':'ALL' }
driver = webdriver.Chrome()
driver.get("http://192.168.0.3:3333/")
while True:
    time.sleep(0.1)
    elem = driver.find_element_by_id('keyvalue')
    abc = elem.get_attribute("value")
    print (abc)
    time.sleep(0.1)
    shell.SendKeys(abc, 0)
    driver.execute_script("document.getElementById('keyvalue').value = ''")

EDIT: Refined code to work much nicer, registering ever button press rather than a time delay.