简体   繁体   English

从铬到硒的console.log

[英]console.log from chrome to selenium

I have most of the code working for this now im just looking for selenium to get a console.log from Chrome 我现在大多数代码都在为此工作,我只是在寻找硒来从Chrome获取console.log。

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. 我得到的问题是'webdriver'没有属性'get_log()',我尝试使用'driver.ChromeOptions.get_log()'但没有属性ChromeOptions。

im a little lost with this module as i have tried example code with the same results. 我对该模块有点迷茫,因为我尝试了具有相同结果的示例代码。

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. 我不是使用console.log而是使用div的值并使用onclick js函数,因此将值设置为“ key1”,然后使用另一个js函数以250ms的延迟清除该值。 (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. 硒代码基本上检查div的值,并每隔0.1秒打印一次。

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. 编辑:改进的代码工作得更好,注册了按钮的按下而不是时间延迟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM