1

Hello everyone I am trying to print to the console the html of url. I took the code from tutorial.py that free in the open source. That is the class:

class LoadHandler(object):
    def OnLoadingStateChange(self, browser, is_loading, **_):
        """Called when the loading state has changed."""
        if not is_loading:
            # Loading is complete. DOM is ready.
            # js_print(browser, "Python", "OnLoadingStateChange", "Loading is complete")
            print('ready')
            print(browser.GetMainFrame().GetText())

and I added the two last lines:

print('ready')
print(browser.GetMainFrame().GetText())

When I run the code I get an ERROR massage:

TypeError: GetText() takes exactly one argument (0 given)

I saw in the documentation that I need to give to the function parameter StringVisitor (https://github.com/cztomczak/cefpython/blob/master/api/Frame.md#gettext)

What is the StringVisitor and how can I solve this problem?

1 Answers1

1

StringVisitor is an object of a class that implemented Visit() method. Below is what you want to do:

class Visitor(object)
    def Visit(self, value):
        print(value)
myvisitor = Visitor()
class LoadHandler(object):
    def OnLoadingStateChange(self, browser, is_loading, **_):
        """Called when the loading state has changed."""
        if not is_loading:
            # Loading is complete. DOM is ready.
            print('ready')
            browser.GetMainFrame().GetText(myvisitor)

It looks strange to put myvisitor outside the OnLoadingStateChange() function but it is one of the many ways to keep that object alive after the GetText() function returns, for the reason that GetText() is asynchronous.

You need to use StringVisitor in cefpython because many CEF functions are asynchronous, namely, then return immediately without finishing what you want them to do. They will call your callback functions when the actual work is done. In your example, when the text prepared by GetText() is ready, it will call the Visit() method in your StringVisitor object. This also means you need to think the other way in your program's flow.

(I have answered a similar question in Need to get HTML source as string CEFPython)

adrtam
  • 6,991
  • 2
  • 12
  • 27