GithubHelp home page GithubHelp logo

fate0 / pychrome Goto Github PK

View Code? Open in Web Editor NEW
591.0 13.0 110.0 936 KB

A Python Package for the Google Chrome Dev Protocol [threading base]

License: Other

Python 100.00%
chrome-debugging-protocol python chrome headless chrome-devtools headless-chrome pychrome

pychrome's Introduction

pychrome

Build Status Codecov Updates PyPI PyPI

A Python Package for the Google Chrome Dev Protocol, more document

Table of Contents

Installation

To install pychrome, simply:

$ pip install -U pychrome

or from GitHub:

$ pip install -U git+https://github.com/fate0/pychrome.git

or from source:

$ python setup.py install

Setup Chrome

simply:

$ google-chrome --remote-debugging-port=9222

or headless mode (chrome version >= 59):

$ google-chrome --headless --disable-gpu --remote-debugging-port=9222

or use docker:

$ docker pull fate0/headless-chrome
$ docker run -it --rm --cap-add=SYS_ADMIN -p9222:9222 fate0/headless-chrome

Getting Started

import pychrome

# create a browser instance
browser = pychrome.Browser(url="http://127.0.0.1:9222")

# create a tab
tab = browser.new_tab()

# register callback if you want
def request_will_be_sent(**kwargs):
    print("loading: %s" % kwargs.get('request').get('url'))

tab.Network.requestWillBeSent = request_will_be_sent

# start the tab 
tab.start()

# call method
tab.Network.enable()
# call method with timeout
tab.Page.navigate(url="https://github.com/fate0/pychrome", _timeout=5)

# wait for loading
tab.wait(5)

# stop the tab (stop handle events and stop recv message from chrome)
tab.stop()

# close tab
browser.close_tab(tab)

or (alternate syntax)

import pychrome

browser = pychrome.Browser(url="http://127.0.0.1:9222")
tab = browser.new_tab()

def request_will_be_sent(**kwargs):
    print("loading: %s" % kwargs.get('request').get('url'))


tab.set_listener("Network.requestWillBeSent", request_will_be_sent)

tab.start()
tab.call_method("Network.enable")
tab.call_method("Page.navigate", url="https://github.com/fate0/pychrome", _timeout=5)

tab.wait(5)
tab.stop()

browser.close_tab(tab)

more methods or events could be found in Chrome DevTools Protocol

Debug

set DEBUG env variable:

pychrome_with_debug_env

Tab management

run pychrome -h for more info

example:

pychrome_tab_management

Examples

please see the examples directory for more examples

Ref

pychrome's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pychrome's Issues

Initial Update

Hi 👊

This is my first visit to this fine repo, but it seems you have been working hard to keep all dependencies updated so far.

Once you have closed this issue, I'll create separate pull requests for every update as soon as I find one.

That's it for now!

Happy merging! 🤖

Browser.current_tab()

This method would be nice to have:

def current_tab(self):
    self.list_tab()
    pages = requests.get("%s/json" % self.dev_url, json=True)
    for p in pages.json():
        if p['type'] == 'page':
            return self._tabs[p['id']]

Daemon problem? Might be me.

Exception in thread Thread-6 (most likely raised during interpreter shutdown):
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
File "/usr/lib/python2.7/threading.py", line 754, in run
File "/usr/local/lib/python2.7/dist-packages/pychrome/tab.py", line 151, in _handle_event_loop
<type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'Empty'

I'm using an altered version of your threading example. Here's a full gist. https://gist.github.com/matthewlilley/aee00d0f05d6c4f95b34774f0c846a88

It's likely something I'm doing wrong, but it always comes back to tab.py.

Any help would be appreciated.

连续运行3小时左右,程序就会卡死

已经发生好几次,连续运行3小时左右,程序就会卡死,ctrl+c中止了程序,就得到了下面的报错信息,好像是线程超时了?

Traceback (most recent call last):
  File "test.py", line 174, in <module>
    test()
  File "test.py", line 122, in test
    r.script.load_jQuery_script("jquery-3.4.1.slim.min.js")
  File "/root/code/robot/script.py", line 37, in load_jQuery_script
    res = self.run_script(script, 3000)
  File "/root/code/robot/script.py", line 22, in run_script
    expression=script, returnByValue=True, timeout=timeout
  File "/usr/local/lib/python3.7/site-packages/pychrome/tab.py", line 174, in call_method
    result = self._send({"method": _method, "params": kwargs}, timeout=timeout)
  File "/usr/local/lib/python3.7/site-packages/pychrome/tab.py", line 106, in _send
    return self.method_results[message['id']].get(timeout=q_timeout)
  File "/usr/local/lib/python3.7/queue.py", line 179, in get
    self.not_empty.wait(remaining)
  File "/usr/local/lib/python3.7/threading.py", line 300, in wait
    gotit = waiter.acquire(True, timeout)

我使用docker环境,python:3.7+alpeware/chrome-headless-stable:latest

Date object return by runtime is {}

tab.Runtime.evaluate(expression="new Date()", returnByValue=True)
it returns {'result': {'type': 'object', 'value': {}}} but not a datetime object, is there anyway to return date object but not transfer by timestamp ?

Something broken with newest chrome with tabs managment

Running example multi_tabs_navigate.py gives error:


Traceback (most recent call last):
  File "./2.py", line 97, in <module>
    main()
  File "./2.py", line 70, in main
    close_all_tabs(browser)
  File "./2.py", line 64, in close_all_tabs
    assert len(browser.list_tab()) == 0
AssertionError

If i comment assert statement I get antother error:

Traceback (most recent call last):
  File "./2.py", line 97, in <module>
    main()
  File "./2.py", line 74, in main
    tabs.append(browser.new_tab())
  File "/usr/local/lib/python2.7/dist-packages/pychrome/browser.py", line 28, in new_tab
    tab = Tab(**rp.json())
  File "/usr/local/lib/python2.7/dist-packages/requests/models.py", line 892, in json
    return complexjson.loads(self.text, **kwargs)
  File "/usr/lib/python2.7/dist-packages/simplejson/__init__.py", line 518, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 370, in decode
    obj, end = self.raw_decode(s)
  File "/usr/lib/python2.7/dist-packages/simplejson/decoder.py", line 400, in raw_decode
    return self.scan_once(s, idx=_w(s, idx).end())
simplejson.errors.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

Last one just after calling new_tab()

Calling Page.navigate timeout

我使用 Page.navigate 重复打开页面,运行一段时间后,就会出现“Calling Page.navigate timeout”这个错误,这是什么原因?

获取网络返回信息时报错

def loadingFinished(**kwargs):
    print('req is ',kwargs.get('requestId'),kwargs)
    _response = tab.Network.getResponseBody(requestId=kwargs.get('requestId'))
    _response_content = _response.get('body')
    print(_response_content[:100])
tab.set_listener('Network.loadingFinished',loadingFinished)```


pychrome.exceptions.CallMethodException: calling method: Network.getResponseBody error: No resource with given identifier found

Chrome update from v71 to v72 broke request interception inside cross-domain iframes

Here's some minimal code to demonstrate the problem. In chrome 71, every request is intercepted and printed. In chrome 72, requests inside cross domain iframes do not even get to "Hello World"

import pychrome
REQUEST_INTERCEPT_PATTERNS = [{"urlPattern": "*", "interceptionStage": "HeadersReceived"}]
def request_intercepted(interceptionId, request, **kwargs):
    print('HELLO WORLD')
    body = tab.Network.getResponseBodyForInterception(interceptionId=interceptionId)['body']
    print(body)
    tab.Network.continueInterceptedRequest(interceptionId=interceptionId)
    

if __name__ == '__main__':
    browser = pychrome.Browser(url="http://127.0.0.1:9200")
    tab = browser.list_tab()[0]
    tab.Network.requestIntercepted = request_intercepted
    tab.start()
    tab.Page.enable()
    print(tab.Network)
    tab.Network.setRequestInterception(patterns=REQUEST_INTERCEPT_PATTERNS)

Blocking Page.Navigate

Page.Navigate appears to be blocking. How would you utilise Network.requestIntercepted and related Network.continueInterceptedRequest in this case? All other events appear to be queued, which is not real-time.

Would it be best to attempt to multithread the navigate command?

Is it possible to get websocket data?

With Chrome in general, I can see the websocket data from Chrome dev tools - network tab - websocket request - Frame tab.

Is it possible to get these websocket data via pychrome If i go to the url where using websocket?

cannot release un-acquired lock

while i import pychrome , see this issue

<ipython-input-1-2c3f630e6c39> in <module>()
----> 1 import pychrome

//anaconda/lib/python3.6/importlib/_bootstrap.py in _find_and_load(name, import_)

//anaconda/lib/python3.6/importlib/_bootstrap.py in __exit__(self, *args, **kwargs)

//anaconda/lib/python3.6/importlib/_bootstrap.py in release(self)

RuntimeError: cannot release un-acquired lock

new_tab() raise error

It raise the error while I run the example demo1.py

Traceback (most recent call last): File "/home/xxx/workspace/py/pychrome_test/src/demo1.py", line 9, in <module> tab = browser.new_tab() File "/home/xxx/workspace/py/pychrome_test/venv/local/lib/python2.7/site-packages/pychrome/browser.py", line 28, in new_tab tab = Tab(**rp.json()) File "/home/xxx/workspace/py/pychrome_test/venv/local/lib/python2.7/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib/python2.7/json/__init__.py", line 339, in loads return _default_decoder.decode(s) File "/usr/lib/python2.7/json/decoder.py", line 364, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib/python2.7/json/decoder.py", line 382, in raw_decode raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded

I found in new_tab method,
requests.get("%s/json/new?%s" % (self.dev_url, url), json=True, timeout=timeout) return 401(http status code)

gevent.monkey.patch_thread() causes errors when threading before importing

I am opening an MongoDB client before importing pychrome.

from pymongo import MongoClient
c = MongoClient("12.12.12.12", 12345)
import pychrome

The following error is observed

{'_MongoClient__options': <pymongo.client_options.ClientOptions object at 0x1019f51d0>, '_MongoClient__default_database_name': None, '_MongoClient__lock': <unlocked _thread.lock object at 0x101975f08>, '_MongoClient__cursor_manager': None, '_MongoClient__kill_cursors_queue': [], '_event_listeners': <pymongo.monitoring._EventListeners object at 0x1019f5b38>, '_MongoClient__index_cache': {}, '_MongoClient__index_cache_lock': <unlocked _thread.lock object at 0x101975e18>, '_BaseObject__codec_options': CodecOptions(document_class=dict, tz_aware=False, uuid_representation=PYTHON_LEGACY, unicode_decode_error_handler='strict', tzinfo=None), '_BaseObject__read_preference': Primary(), '_BaseObject__write_concern': WriteConcern(), '_BaseObject__read_concern': ReadConcern(), '_MongoClient__all_credentials': {}, '_topology_settings': <pymongo.settings.TopologySettings object at 0x10207ac50>, '_topology': <pymongo.topology.Topology object at 0x102247e80>, '_kill_cursors_executor': <pymongo.periodic_executor.PeriodicExecutor object at 0x10224c978>}
Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/jimmytran/.virtualenvs/crypto/lib/python3.6/site-packages/pychrome/browser.py", line 11, in <module>
    from pychrome.tab import Tab
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 154, in __exit__
  File "<frozen importlib._bootstrap>", line 106, in release
RuntimeError: cannot release un-acquired lock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/jimmytran/.virtualenvs/crypto/lib/python3.6/site-packages/pychrome/__init__.py", line 6, in <module>
    from pychrome.browser import Browser
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 154, in __exit__
  File "<frozen importlib._bootstrap>", line 106, in release
RuntimeError: cannot release un-acquired lock

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/jimmytran/Workspace/crypto/crypto/__main__.py", line 5, in <module>
    import pychrome
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 154, in __exit__
  File "<frozen importlib._bootstrap>", line 106, in release
RuntimeError: cannot release un-acquired lock

Removing #gevent.monkey.patch_thread() in tab.py solves the issue.

import gevent.monkey
gevent.monkey.patch_socket()
# gevent.monkey.patch_thread()

Simulate clicks?

When sending POST requests, you need to carry parameters.
So, Can you skip this part and directly simulate clicks?

Thanks, Sincerely.

v0.2.0 todo list

  • offer REPL interface like chrome-remote-interface (maybe require IPython ?)
  • offer stable API
  • documents and examples are never enough : )
  • add pychrome to awesome-chrome-devtools list
  • switch to gevent base

I found NodeId ,how can i click it

    eh = ChromeEventHandler(self.Tab, self.SearchURL)
    self.Tab.Network.responseReceived = eh.response_Received
    self.Tab.Network.requestIntercepted = eh.request_intercepted
    self.Tab.Network.enable()
    self.Tab.Network.setRequestInterceptionEnabled(enabled=True)
    self.Tab.DOM.enable()
    self.Tab.DOM.getDocument()
    element = self.Tab.DOM.performSearch(query='xpath',includeUserAgentShadowDOM=True)
    print(element )
    element__count = int(element.get('resultCount'))
    element__search_id = elemen.get('searchId')
    getSearchResults = self.Tab.DOM.getSearchResults(searchId= element__search_id, fromIndex=0,toIndex=element__count)
  print( getSearchResults)
   nodeId=NodeIds[X]

output:
performSearch={"searchId": "14340.339", "resultCount": 1}
getSearchResults={"nodeIds": [127]}
NodeIds=[127]

now, how can i use the element(by nodeId) click method?
thx

How to tracing?

I'm trying to trace my JS code but i can't get my performance mark in trace log.
My JS code:

<script>
  performance.mark('my-start');

  for (let i = 0; i < 5; i++) {
    for (let j = 0; j < 2; j++) {
      a = Math.cbrt(Math.random() * 100);
      console.log(a);
    }         
  }
  performance.mark('my-end');
  performance.measure('my-benchmark', 'my-start', 'my-end');
</script>

Python code:

import pychrome
import json

browser = pychrome.Browser(url="http://127.0.0.1:9222")

tab = browser.new_tab()
open('trace_log.json', 'w').close()

def tracing_complete(**kwargs):
    eof = False
    while not eof:
        data = tab.call_method("IO.read", handle=kwargs["stream"])
        eof = data["eof"]
        if data["data"]:
            data = json.loads(str(data["data"]))
            with open('data.json', 'a') as f:
                json.dump(data, f, indent=4)

    tab.call_method("IO.close", handle=kwargs["stream"])

tab.set_listener("Tracing.tracingComplete", tracing_complete)

tab.start()
tab.call_method("Network.enable")

tab.call_method("Tracing.start", transferMode='ReturnAsStream', _timeout=5)
tab.call_method("Page.navigate", url="my_page.html", _timeout=5)
tab.call_method("Tracing.end",  _timeout=5)

tab.wait(5)
tab.stop()
browser.close_tab(tab)

It seems what i'm dooing something wrong since i can get my mark using chrome performance profiling.

CallMethodException: calling method: Page.printToPDF error: PrintToPDF is not implemented

I try to use tab.Page.printToPDF() but it fails complaining its not implemented:

>>> import pychrome                                                         
>>> browser = pychrome.Browser(url="http://127.0.0.1:9222")                 
>>> tab = browser.new_tab()                                                 
>>> tab.start()                                                             
True
>>> tab.Page.navigate(url="file:///home/franck/Desktop/svg/hello.svg")      
{'frameId': '6AB6826A1AD497075326A65B53088ACD',
 'loaderId': '1044382648C5CBD3CA18C80DFABA79B2'}
>>> tab.Page.printToPDF()                                                   
/home/franck/.anaconda3/lib/python3.7/site-packages/pychrome/tab.py:176: UserWarning: Page.printToPDF error: PrintToPDF is not implemented
  warnings.warn("%s error: %s" % (_method, result['error']['message']))
---------------------------------------------------------------------------
CallMethodException                       Traceback (most recent call last)
<ipython-input-6-077e1649bd93> in <module>
----> 1 tab.Page.printToPDF()

~/.anaconda3/lib/python3.7/site-packages/pychrome/tab.py in call_method(self, _method, *args, **kwargs)
    175         if 'result' not in result and 'error' in result:
    176             warnings.warn("%s error: %s" % (_method, result['error']['message']))
--> 177             raise CallMethodException("calling method: %s error: %s" % (_method, result['error']['message']))
    178 
    179         return result['result']

CallMethodException: calling method: Page.printToPDF error: PrintToPDF is not implemented

I have Python 3.7.4, and installed pychrome 0.2.3 from the git repo. I've had this error for both google-chrome and chromium, using Linux.

Thanks for your help!

It seems the doc is wrong about method: tab.wait(5)

In the document https://fate0.github.io/pychrome/#getting-started there is sample code

# start the tab 
tab.start()
tab.Page.navigate(url="https://github.com/fate0/pychrome", _timeout=5)

# wait for loading
tab.wait(5)

# stop the tab (stop handle events and stop recv message from chrome)
tab.stop()

But when I look inside the pychrome code. wait is defined like this

    def wait(self, timeout=None):
        if not self._started:
            raise RuntimeException("Tab is not running")

        if timeout:
            return self._stopped.wait(timeout)

        self._recv_th.join()
        self._handle_event_th.join()
        return True

The wait should be described as wait for thread stopped, So I think the right document should be

# start the tab 
tab.start()
tab.Page.navigate(url="https://github.com/fate0/pychrome", _timeout=5)


# stop the tab (stop handle events and stop recv message from chrome)
tab.stop()

# wait for thread handle stopped
tab.wait(5)

headerTemplate and footerTemplate doesn't work in printToPDF

python version: tried 2.7, 3.7
pychrome version: 0.2.2

Using examples/multi_tabs_pdf.py

Modified printing part:

Page.printToPDF(
    displayHeaderFooter=True,
    printBackground=False,
    landscape=False,
    scale=1,
    marginTop=2.0833333333333335,
    marginBottom=2.0833333333333335,
    paperWidth=8.5,
    paperHeight=11,
    marginLeft=0,
    marginRight=0,
    pageRanges="",
    headerTemplate="<h1>Header text</h1>",
    footerTemplate="<h1>Footer text</h1>"
)

pychrome debug SEND ►

{
  "method": "Page.printToPDF",
  "params": {
    "displayHeaderFooter": true,
    "printBackground": false,
    "landscape": false,
    "scale": 1,
    "marginTop": 2.0833333333333335,
    "marginBottom": 2.0833333333333335,
    "paperWidth": 8.5,
    "paperHeight": 11,
    "marginLeft": 0,
    "marginRight": 0,
    "pageRanges": "",
    "headerTemplate": "<h1>Header text</h1>",
    "footerTemplate": "<h1>Footer text</h1>"
  },
  "id": 1005
}

As a result pdf is rendered but with only default header and footer.

Additional note:
When using puppeteer

On same page with exactly same parameters, it works
puppeteer debug SEND ► output

{
  "id": 12,
  "method": "Page.printToPDF",
  "params": {
    "landscape": false,
    "displayHeaderFooter": true,
    "headerTemplate": "<h1>Header text</h1>",
    "footerTemplate": "<h1>Footer text</h1>",
    "printBackground": false,
    "scale": 1,
    "paperWidth": 8.5,
    "paperHeight": 11,
    "marginTop": 2.0833333333333335,
    "marginBottom": 2.0833333333333335,
    "marginLeft": 0,
    "marginRight": 0,
    "pageRanges": ""
  }
}

how to get the html source code of a dynamic website

`import pychrome

browser = pychrome.Browser(url="http://127.0.0.1:9222")
tab = browser.new_tab()
def request_will_be_sent(**kwargs):
print("loading: %s" % kwargs.get('request').get('url'))
tab.Network.requestWillBeSent = request_will_be_sent
tab.start()
tab.Network.enable()
tab.Page.enable()
bbb = tab.Page.navigate(url="http://book.jd.com/booktop/0-0-0.html?category=1713-0-0-0-10001-1#comfort", _timeout=5)
frameId = bbb["frameId"]
print(frameId)
aaa = tab.Page.getResourceContent(frameId=frameId, url="http://book.jd.com/booktop/0-0-0.html?category=1713-0-0-0-10001-1#comfort")
print(aaa)
tab.wait(5)
tab.stop()
browser.close_tab(tab)`

with this code, it doesnot get the final html source code.

how can i get a response in a middle url? i have try some `Network.responseReceived` but it can not work.

code:

    9 # register callback if you want
   10 def request_will_be_sent(**kwargs):
   11     with open("url.txt", 'a') as f:
   12     ¦   f.write(kwargs.get('request').get('url') + '\n')
   13 
   14 tab.Network.requestWillBeSent = request_will_be_sent
   15 
   16 def response_received(**kwargs):
   17     with open('1.txt', 'a') as f:
   18     ¦   f.write(json.dumps(kwargs))
   19 
   20 tab.Network.responseReceived = response_received

and the Network.responseReceived can't work, the output is:

No handlers could be found for logger "pychrome.tab"

hope for your answer

Changing the window size

Hi, new to this library, how would you go about changing the window size? As in Browser.setWindowBounds?

Also downloading a file?

TIA

Async processing for some events

Hi!
Good job, and there is a lot of wit in it. The two-thread solution is great.

But I found two types of events, that should be processed in recv-thread, not in an event one.

  1. Inspector.targetCrashed -- tab crash. Problem is: after this event there will be no answers for waiting calls (not even with errors -- nothing! on chromium 66.0.3359.181 at least), so if you just put it in the event queue, you may not get it after all. Your still working event handlers may hang up, waiting for an answer. So after Inspector.targetCrashed have arrived, you should call stop() immediately in the _recv_loop function.

  2. Calling methods in not-main targets (chrome terminology for threads). You call it through method Target.sendMessageToTarget and get an answer through event Target.receivedMessageFromTarget. The problem is: if you are in an event-handler and you operate in one thread, you can not just wait for an answer -- it will never come. It will come in recv, and will be enqueued, but would not be processed until your current handler is finished -- it is a deadlock.

There is a workaround of cause. You can start a separate thread for every event handler, that call any API methods. But it is an ugly solution.

I suggest something like that:

crashed_tab.txt

I actually use it now, and it seems to work ok.
Maybe you can adapt this idea and use it in your library?

P.S. For clarity. The "idea" here is just some possibility to install recv-time handlers for messages. All other just implementation details and example.

how to get whole page loaded event?

I have tried both:
tab.Page.loadEventFired = loadEventFired
tab.set_listener("Page.loadEventFired", loadEventFired)
None of them works.
I turn DEBUG on and just get:
◀ RECV {"method":"Network.dataReceived","params":{"requestId":"9480.27","timestamp":145931.801829,"dataLength":43,"encodedDataLength":0}}
◀ RECV {"method":"Network.dataReceived","params":{"requestId":"9480.27","timestamp":145931.802118,"dataLength":0,"encodedDataLength":43}}
◀ RECV {"method":"Network.loadingFinished","params":{"requestId":"9480.27","timestamp":145931.800791,"encodedDataLength":299}}

just Network.loadingFinished event, no Page.loadEventFired

tab.Page.addScriptToEvaluateOnNewDocument not working?

I can not get this method to work at all. It shows in the debug log and I am given an id and identifier response, but the scripts never seem to be executed when I load pages. I'm not really sure how this issue is specific to PyChrome, but the same method is working on the same Chromium setup with Selenium, so I think it must be.

To reproduce, just try adding something like this to the demo program and you'll be able to see that the console receives no alerts:

tab.Page.addScriptToEvaluateOnNewDocument(source='console.log("test")')

How can I use proxy

If I open a url must use third proxy,how can I do? I can not find a better way but only use proxy parameter when I start to run chrome progress.

Creating RequestPattern object

Does pychrome support creating Chrome objects?

I've tried creating RequestPattern to later use in Network.setRequestInterception but got greeted with an error:

tab_handle.Network.RequestPattern()

/usr/local/lib/python3.4/dist-packages/pychrome/tab.py in call_method(self, _method, *args, **kwargs)
    173         if 'result' not in result and 'error' in result:
    174             warnings.warn("%s error: %s" % (_method, result['error']['message']))
--> 175             raise CallMethodException("calling method: %s error: %s" % (_method, result['error']['message']))
    176 
    177         return result['result']

CallMethodException: calling method: Network.RequestPattern error: 'Network.RequestPattern' wasn't found

tab_handle.RequestPattern() also does not work:

tab_handle.RequestPattern()

TypeError: 'GenericAttr' object is not callable

Error in multi_tabs_navigate.py example

tab.Network.setRequestInterceptionEnabled(enabled=True)

I get the following error when the above line is hit:

/Users/XXX/Work/py3imports/lib/python3.6/site-packages/pychrome/tab.py:174: UserWarning: Network.setRequestInterceptionEnabled error: 'Network.setRequestInterceptionEnabled' wasn't found warnings.warn("%s error: %s" % (_method, result['error']['message'])) Traceback (most recent call last): File "test.py", line 98, in <module> main() File "test.py", line 86, in main tab.Network.setRequestInterceptionEnabled(enabled=True) File "/Users/XXX/Work/py3imports/lib/python3.6/site-packages/pychrome/tab.py", line 175, in call_method raise CallMethodException("calling method: %s error: %s" % (_method, result['error']['message'])) pychrome.exceptions.CallMethodException: calling method: Network.setRequestInterceptionEnabled error: 'Network.setRequestInterceptionEnabled' wasn't found

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.