How to simulate clicking a link

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dbphydb
    New Member
    • Apr 2010
    • 22

    How to simulate clicking a link

    Hi,

    Im basically from QA. I have a piece of code in python that parses HTML pages to reach to a specific link on a webpage. The below code gets me the url of the link which needs to be clicked. What code should i write to simulate clicking a link.

    Code:
    URL = "http://11.12.13.27:8080/cruisecontrol"
    BRANCH_FILE = "b.txt"
    
    from urllib2 import urlopen
    from HTMLParser import HTMLParser
    
    import re
    destination_re = re.compile(r"Test 5")
    
    def read_branch():
        return open(BRANCH_FILE).read().strip()
    
    def get_links(url):
        parser = MyHTMLParser()
        parser.feed(urlopen(url).read())
        parser.close()
        return parser.links
    
    # Build url for Deploy page
    def get_deploy_url():
        branch = read_branch().lower()
        url = URL + "/buildresults/abc_%s_nightly_build" % branch
        for link in get_links(url):
            if link["href"].startswith("Deploy"):
                return "%s/%s" % (URL, link["href"])
    
    # Build url for Destination page
    def get_destination_url():
        url = get_deploy_url()
        print url
        for link in get_links(url):
            if destination_re.search(link["href"]):
                return "http://11.12.13.27:8080/cruisecontrol/" + link["href"]
    
    
    class MyHTMLParser(HTMLParser):
        def __init__(self, *args, **kwd):
            HTMLParser.__init__(self, *args, **kwd)
            self.links = []
    
        def handle_starttag(self, tag, attrs):
            if tag == "a":
                attrs = dict(attrs)
                if "href" in attrs:
                    self.links.append(dict(attrs))
    
        def handle_endtag(self, tag):
            pass
    
    if __name__ == "__main__":
        final_url = get_destination_url()
        if final_url is None:
            print "Could not find a destination to deploy"
        else:
            print final_url

    Below is the output i get
    http://11.12.13.27:808 0/cruisecontrol/DeploySelect.js p?ArtifactsUrl= artifacts/abc_b_nightly_b uild/20100506124231

    http://11.12.13.27:808 0/cruisecontrol/DeployConfig.js p?name=Test 5&scriptPath=xx xxxxxxxxx


    Actually i have a need to simulate clicking Test 5 link on the deployment webpage url. Please help.
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    When I've done this in the past, I've simply extracted the link command and sent that.

    For example, there was a form which searched for data on particular shares. The UI was that you'd type a code into a box and press a button. But looking at the page source revealed that the button took the data from the text box, appended it to a .asp command and went to that website.

    So my code just did this
    Code:
    url='http://www.sharedata.co.za/findshare.asp?SearchString='+share
    req=urllib2.Request(url)
    Of course, in principle, you could find the action that the button does (with a regular expression or something) and make that your variable, and "press" the button by then going to the relevant link. That's typically all that's happening behind the scenes anyway...

    Comment

    Working...