【问题标题】:Is there a way to execute and read data from Chrome's console using python?有没有办法使用 python 从 Chrome 的控制台执行和读取数据?
【发布时间】:2017-10-14 21:07:47
【问题描述】:
我正在尝试找到一种从 Chrome 控制台读取信息的方法。我使用的网站是chrome://Dino。在控制台中,我要执行和读取的主要命令是Runner().crashed、Runner().horizon.runningTime、Runner().horizon.obstacles,以及其他一些简单的命令。
我尝试过使用 Selenium,但我无法让它工作,并且在谷歌搜索了一段时间后,我找不到解决方案。你知道我是怎么做到的吗?
【问题讨论】:
标签:
python-3.x
google-chrome
selenium
selenium-chromedriver
【解决方案1】:
我能够使用 Selenium(使用 C#)来获取所有列出的信息。
'How to output result of Javascript execution to console?' 给了我一个提示。
// initialize the ChromeDriver, and get the jsExecutor
// This step may be different for Python
var chromeDriver = new ChromeDriver();
var jSExecutor = (IJavaScriptExecutor)chromeDriver;
// navigate to the page
chromeDriver.Navigate().GoToUrl("chrome://dino");
// get the values, using js command
var crashed = jSExecutor.ExecuteScript("return Runner().crashed");
var runningTime = jSExecutor.ExecuteScript("return Runner().horizon.runningTime");
var obstcles = jSExecutor.ExecuteScript("return Runner().horizon.obstacles");
// print the values
Console.WriteLine($"{crashed}, {runningTime}, {obstcles}");
应该可以在 Python 中使用driver.execute_script 来做同样的事情。
crashed = driver.execute_script('return Runner().crashed')
runningTime = driver.execute_script('return Runner().horizon.runningTime')
obstcles = driver.execute_script('return Runner().horizon.obstacles')