Selenium w/ Java Problem with ASP.Net Form with __doPostBack()

65 Views Asked by At

I am Newb learning Selenium w/ Java to test and scrape web pages. I have successfully established Eclipse connection to mysql DB, loaded selenium and applicable jars, can leverage code to navigate to:

  • Target Site (homepage)
  • select url and navigate to next page
  • select url and navigate to next page
  • this 3rd page has an "Accept" button that I can not get "past" as if I'm a user to clicking the button. I can:
  • manually click the Accept button and advance to target page
  • w/out moving the mouse, let the page load and manually hit the enter key and advance
  • I can even retrieve the target url and leverage java and selenium to skip the Accept Page and successfully navigate to the target page.

.......but I don't want to circumnavigate the problem, I want to learn how to get past this problem. The fun of learning is quickly dwindling as I have spent weeks (hours after work) trying to google and figure this out only to no avail. That being said, I feel like I'm getting close, but can't get over the finish line.

What I have leanred:

  • Dev Tools are pretty cool and starting to grasp, elements, id's, names, xpath (relative and absolute).
  • Learned about SelectorsHub
  • I've learned the target site involves an ASP.net page / form that kicking my A**
  • I believe there are two hidden fields that need to pass data to the dopostback post
  • I believe that elements are being hidden as numerous attempts to set WebElement equal to ID's, names, xpath result in unable to locate element (using xpath) or no such element exception (and a myriad of other 'misses' at attempting to "click" the "Accept" button.
  • In addition to attempting to set Web Element and click, I've researched and attempted to implement wait code (see Thread.sleep below) as lots of articles suggest elements are delayed in loading, I've tried JavascriptExecutor (which looks promising and I'm guessing will be the way to go eventually), as well as implementations of WebDriverWait, all to no avail.

Base code that let's me succesfully navigate around is below:

WebElement          WE;
private static WebDriver    g_WD;
private static int      g_intSleep = 2000;

public void Main  () throws InterruptedException    {
    System.setProperty("webdriver.firefox.driver", "/usr/bin/firefox");
    g_WD  =  new FirefoxDriver();

    S_05_AcceptDisclaimer ();
}

private void  S_05_AcceptDisclaimer () {
    WebElement      WE;
    String          strElementID;

    try {
        Thread.sleep(g_intSleep);
        Thread.sleep(g_intSleep);
        Thread.sleep(g_intSleep);
        Thread.sleep(g_intSleep);
        strElementID =                     "/html[1]/body[1]/form[1]/div[3]/div[2]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/p[4]/a[1]";
        
        WE = g_WD.findElement(By.xpath(strElementID));
        WE.click();
    } 
    catch (InterruptedException e) 
    {
        e.printStackTrace();
    }
}

Inspection of form data reveals the following:

<a id="ctl00_Content1_button_accept" class="button_imaged" onfocus="highlight(this);"           onblur="doBlur(this);" href="javascript:__doPostBack('ctl00$Content1$button_accept','')"><img id="ctl00_Content1_Image2" src="images/icons/page_go.png" align="absmiddle">
      Accept</a>
<img id="ctl00_Content1_Image2" src="images/icons/page_go.png" align="absmiddle">

and I'm also drawn to the following hidden elements

<input type="hidden" name="ctl00$Content1$inq_id" id="ctl00_Content1_inq_id" value="fci">
<input type="hidden" name="ctl00$Content1$nav_url" id="ctl00_Content1_nav_url" value="inquiry.aspx?ty=CI">

...and learning about Dev Tools and playing the role of Matlock trying to figure out root cause analysis of the problem, I see these two (2) variables in play w/ the form:

enter image description here

enter image description here

So based on my digging, I believe next steps are to focus on the call that the form is making (doPostback) or that I need to make back to the form, which appears to be calling a javascript routine

(href="javascript:__doPostBack('ctl00$Content1$button_accept',''))

inclusive of the 2 parameters (String_id and nav_url) which result in a querystring hyperlink to inquiry.aspx?ty-CI....which ....if I just plug in a the url to a WebDriver get command, I can get there. But I want to learn how to mimic the appropriate call on the standard path / page to get to the correct end result. My suspicion is that I need to go w/ some sort of javascriptExecutor call somehow back to the form, passing the two (2) variable / key parameters, and the site will direct me to the correct page. I feel this will be an important lesson down the road when I run into these types of forms again. I apologize in advance for the lengthy post, but trying to be thorough / precise. I wish this was more entertaining like a Rodney Dangerfield skit, but I've reached diminishing marginal return on how much time I'm spending trying to youtube and google my way through the problem.

Any all help is greatly appreciated !!!

0

There are 0 best solutions below