2013-10-16 37 views
4

我需要从浏览器自动化脚本向Chrome扩展发送一个值。 我目前试图这样做的方式是通过试图调用来自硒的chrome.runtime.sendMessage API来将某些值传递给chrome扩展。在Python代码是:无法从selenium访问铬消息传递API execute_script

import os 
import time 
from selenium import webdriver 
from selenium.webdriver.common.action_chains import ActionChains 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.chrome.options import Options 



chrome_options = Options() 
chrome_options.add_extension('/home/lurscher/plugin.crx') 
browser = webdriver.Chrome(chrome_options=chrome_options) 
browser.get(url) 
browser.execute_script("chrome.runtime.sendMessage({someValue: "+str(args.value)+"}, function(response) { console.log('value sent. '+response)})") 

我得到这个错误:

Traceback (most recent call last): 
    File "tools/selenium/open_page.py", line 17, in <module> 
    browser.execute_script("chrome.runtime.sendMessage({someValue: "+str(args.value)+"}, function(response) { console.log('value sent. '+response)})") 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 397, in execute_script 
    {'script': script, 'args':converted_args})['value'] 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 165, in execute 
    self.error_handler.check_response(response) 
    File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/errorhandler.py", line 164, in check_response 
    raise exception_class(message, screen, stacktrace) 
selenium.common.exceptions.WebDriverException: Message: u"unknown error: Cannot call method 'sendMessage' of undefined\n (Session info: chrome=28.0.1500.71)\n (Driver info: chromedriver=2.1,platform=Linux 3.5.0-17-generic x86_64)" 

Question: Any idea what I'm doing wrong?

I need to send a value to the chrome extension from the browser automation script. How Do I Do It?

回答

0

由于异常消息称,Cannot call method 'sendMessage' of undefined。 看来chrome.runtime调用只能在chrome扩展的contenxt中调用,而execute_script中的代码只是在页面的上下文中调用。

0

此问题可能有多个解决方案。但是我会建议将内容脚本页面用于您的扩展,并使用内容脚本中的函数进行通信。由于内容脚本构成了客户端部分,因此您可以访问内容脚本中定义的功能,以便与扩展程序进行通信而不会有任何问题。让我知道,如果这不适合你。

0

据我所知你的硒python代码你想发送数据到谷歌chromes扩展。这不可能。你没有权限,Chrome不会允许你这样做。除非你将创建一些隐藏的html元素,例如任何id ='message'的元素,并且在数据属性中或者作为元素的值发送参数。然后在Google Chrome浏览器扩展程序中创建内容脚本,该内容脚本将被注入到加载页面的页面中。内容脚本将被注入页面后,您可以通过给定的ID获取参数。

1

运行这个时候得到了类似的错误:(JavaScript的)

this.driver.executeScript(function() { 
    chrome.runtime.sendMessage('start'); 
}); 
WebDriverError: unknown error: Cannot read property 'sendMessage' of undefined 

在我看来,这chrome.runtime始终可用无论你正在开发的扩展或者只是浏览网页。 (打开一个隐身窗口并在控制台中进行评估;它就在那里)。所以它必须与WebDriver有关。

从我能收集关于interwebs,你必须额外配置驱动程序: https://groups.google.com/forum/#!topic/chromedriver-users/7wF9EHF2jxQ

options.excludeSwitches('test-type'); // this makes chrome.runtime available 
builder.setChromeOptions(options); 

然而,这使得上述错误演变成:

WebDriverError: unknown error: Invalid arguments to connect. 

那是因为你的测试页正在尝试与您的扩展程序进行通信,除非您在清单中声明该页面,否则Chrome不会根据Chrome规范进行通信。例如: -

"externally_connectable": { 
    "matches": [ 
    "http://localhost:8000/mytest.html” 
    ] 
} 

但是你现在必须包括在SendMessage调用扩展ID:

this.driver.executeScript(function() { 
    chrome.runtime.sendMessage('kjnfjpehjfekjjhcgkodhnpfkoalhehl', 'start'); 
}); 

我认为这是一个有点尴尬。

我会推荐一些MGR建议使用内容脚本代理sendMessage调用的内容,因为内容脚本没有外部页面的限制。

我所做的是触发从我的测试,将通过内容脚本这是一个让SendMessage函数调用来拿起一个事件:

在你的测试:

this.driver.executeScript(function() { 
    var event = document.createEvent('HTMLEvents'); 
    event.initEvent('extension-button-click', true, true); 
    document.dispatchEvent(event); 
}); 

申报在您的清单内容脚本:

"content_scripts": [ 
    { "matches": ["<all_urls>"], "js": ["content_script.js"] } 
] 

而且在content_script.js:

document.addEventListener('extension-button-click', function() { 
    chrome.runtime.sendMessage('start'); 
}); 

希望它有帮助