我需要从浏览器自动化脚本向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?