我需要从浏览器自动化脚本向 chrome 扩展发送一个值。我目前尝试这样做的方式是尝试从 selenium 调用chrome.runtime.sendMessage API 以将某些值传达给 chrome 扩展。蟒蛇代码是:
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)"
问题: 知道我做错了什么吗?
我需要从浏览器自动化脚本向 chrome 扩展发送一个值。我该怎么做?