【问题标题】: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().crashedRunner().horizon.runningTimeRunner().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')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-26
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 2017-03-28
    相关资源
    最近更新 更多