Mr_androidviewclient: Getting browser's HTML page source

Source: Internet
Author: User

Standard androidBrowserDoes not provide an option in its menu to view the HTML page source. Some workarounds like installing apps and then usingShare pageFrom
Browser'S menu has been described and also the use of JavaScript and jquery to add to a page has been detailed, but we are hungry for more. needless to say, all these methods involve some manual step so I felt the need to find a completely
Automatic way of doing it.

Of course, to do it I wowould resort to our old pal
Androidviewclient. This is a very interesting example of its use because it lies far from testing and application or UI.

And so, without further ado, let me introduce youThe Code...

#! /usr/bin/env monkeyrunner'''Copyright (C) 2012  Diego Torres MilanoCreated on Oct 12, 2012@author: diego'''import reimport sysimport os# This must be imported before MonkeyRunner and MonkeyDevice,# otherwise the import fails.# PyDev sets PYTHONPATH, use ittry:    for p in os.environ['PYTHONPATH'].split(':'):        if not p in sys.path:            sys.path.append(p)except:    passtry:    sys.path.append(os.path.join(os.environ['ANDROID_VIEW_CLIENT_HOME'], 'src'))except:    passfrom com.dtmilano.android.viewclient import ViewClientfrom com.android.monkeyrunner import MonkeyRunner, MonkeyDeviceVPS = "javascript:alert(document.getElementsByTagName('html')[0].innerHTML);"PACKAGE = 'com.android.browser'ACTIVITY = '.BrowserActivity'COMPONENT = PACKAGE + "/" + ACTIVITYURI = 'http://dtmilano.blogspot.com'device, serialno = ViewClient.connectToDeviceOrExit()device.startActivity(component=COMPONENT, uri=URI)MonkeyRunner.sleep(3)vc = ViewClient(device=device, serialno=serialno)device.drag((240, 180), (240, 420), 10, 10)url = vc.findViewByIdOrRaise('id/url')url.touch()MonkeyRunner.sleep(1)device.press('KEYCODE_DEL', MonkeyDevice.DOWN_AND_UP)for c in VPS:    device.type(c)device.press('KEYCODE_ENTER', MonkeyDevice.DOWN_AND_UP)MonkeyRunner.sleep(3)vc.dump()print vc.findViewByIdOrRaise('id/message').getText().replace('\\n', "\n")device.press('KEYCODE_BACK', MonkeyDevice.DOWN_AND_UP)

And now a brief explanation of the most important pieces of this script.

  1. Shebang, you know, to invoke monkeyrunner as the interpreter. I don't have to tell you more (if you are a poor Windows user you may have to invoke monkeyrunner from command
    Line, I feel sad for you)
  2. Some comments and imports
  3. ReadPythonpathJust in case you are using eclipse and pydev (this has been explained in thispost)
  4. Then useAndroid_view_client_homeEnvironment variable to findAndroidviewclientIn your system
  5. Some constants defined.VPSIs the actual JavaScript to obtain the page source
  6. The standard way of connecting to the device or emulator inAndroidviewclient. This handles errors and timeout automatically solving extends problems you find with bare monkeyrunner
  7. We start Browser
  8. Drag a bit to make the URL visible in case the page has scrolled
  9. Next, we findViewWith IDID/URL, Which you know, contains the URL
  10. We touch to focus
  11. And type the Javascript inVPSFollowedEnter
  12. By that time the alert dialog shocould be on screen so we take a new dump
  13. Now we findViewWith IDID/messageWhich containsHtmlAnd print it
  14. Finally, we pressBackTo dismiss it
I hope you have enjoyed it as much as I did and this help you find new ways of using Androidviewclient. P.s. This script will be part AndroidviewclientSource code distribution examples

Reprinted from: http://dtmilano.blogspot.com/search/label/AndroidViewClient

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.