Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过命令行刷新windows chrome选项卡0(或当前选项卡)_Python_Windows_Google Chrome_Batch File_Python Webbrowser - Fatal编程技术网

Python 通过命令行刷新windows chrome选项卡0(或当前选项卡)

Python 通过命令行刷新windows chrome选项卡0(或当前选项卡),python,windows,google-chrome,batch-file,python-webbrowser,Python,Windows,Google Chrome,Batch File,Python Webbrowser,我正试图用python,webbrowser模块来实现它。但它没有特定于铬的功能。还有别的办法吗?可能是批处理脚本?我自己使用它:(我写得很快,因为它只供个人使用)。经过大量的清理,你可能会得到你想要的。看 导入urllib2 导入URL库 导入操作系统 导入子流程 导入json 从websocket导入创建\u连接 def刷新页面(url): data=json.load(urllib2.urlopen)http://localhost:9222/json')) 找到页面=False 对于数据

我正试图用
python
webbrowser
模块来实现它。但它没有特定于铬的功能。还有别的办法吗?可能是批处理脚本?

我自己使用它:(我写得很快,因为它只供个人使用)。经过大量的清理,你可能会得到你想要的。看

导入urllib2
导入URL库
导入操作系统
导入子流程
导入json
从websocket导入创建\u连接
def刷新页面(url):
data=json.load(urllib2.urlopen)http://localhost:9222/json'))
找到页面=False
对于数据中的页面:
如果页面['url'].lower()==url.lower():
找到页面=True
websocketURL=page['webSocketDebuggerUrl']
ws=创建_连接(websocketURL)
obj={“id”:0,
“方法”:“Page.reload”,
“参数”:
{
“ignoreCache”:正确,
“ScriptToEvaluationLoad”:”
}
}
dev_request=json.dumps(obj)
发送(开发请求)
结果=ws.recv()
ws.close()
如果未在页面中找到:
引发异常(“未找到页面”)
def open_或_refresh(文件名):
file_name=”“.join([f如果r'\/:*?“|”else urllib.quote(f)表示文件_name中的f])
文件名='文件://'+文件名。替换('\\','/'))
file\u name=file\u name.encode('ascii','ignore')
尝试:
刷新页面(文件名)
除:
cmd=(r'”%(LOCALAPPDATA)s\Google\Chrome\Application\Chrome.exe“'%os.environ”
+r'--远程调试端口=9222“%s”%file\u name)
子进程Popen(cmd)
打开或刷新(r“C:\test.html”)
打开或刷新(r“C:\test.html”)

我不确定到底出了什么问题,可能还有另一个问题(一英镑装在一个宣誓罐里,但我能说的是它对我有用。)如果我把open\u或\u refresh(r“C:\test.html”)改成
open\u或\u refresh(u“C:\\test.html”)
你需要字符串是ascii格式。添加
file\u name=file\u name.encode('ascii','ignore')
应该可以解决您的问题。
import urllib2
import urllib
import os
import subprocess
import json

from websocket import create_connection

def refresh_page(url):
    data = json.load(urllib2.urlopen('http://localhost:9222/json'))

    found_page = False
    for page in data:
        if page['url'].lower() == url.lower():
            found_page = True
            websocketURL = page['webSocketDebuggerUrl']
            ws = create_connection(websocketURL)

            obj = {  "id": 0,
                     "method": "Page.reload",
                     "params":
                     {
                       "ignoreCache": True,
                       "scriptToEvaluateOnLoad": ""
                     }
                  }

            dev_request = json.dumps(obj)
            ws.send(dev_request)
            result =  ws.recv()
            ws.close()
    if not found_page:
        raise Exception("No pageFound")

def open_or_refresh(file_name):
    file_name = "".join ( [f if f in r'\/:*?"<>|' else  urllib.quote(f) for f in file_name] )
    file_name = 'file:///' + file_name.replace('\\', '/')
    file_name = file_name.encode('ascii', 'ignore')
    try:
        refresh_page(file_name)
    except:
        cmd = (r'"%(LOCALAPPDATA)s\Google\Chrome\Application\chrome.exe"'%os.environ
               + r' --remote-debugging-port=9222  "%s"' % file_name)
        subprocess.Popen(cmd)

open_or_refresh(r"C:\test.html")
open_or_refresh(r"C:\test.html")