ASPX Form DoPostBack with Java and Selenium

68 Views Asked by At

Test Scenario:

  1. Navigate to target site
  2. Select hyperlink and advance to 2nd page
  3. Select hyperlink on 2nd page and advance to 3rd page
  4. Click on "Accept" button to advance to following page

The Accept Page has a doPostBack javascript hyperlink and I'm unable to use xpath, javascriptExecutor, or any other means to advance to the next page.

Code in question w/ inline comments:

public static void Main ()  {
WebElement      WE;
WebDriver   WD;
WebDriverWait   WW;

System.setProperty("webdriver.gecko.driver", "MyPathTo/geckodriver");
System.setProperty("webdirver.firfox.bin", "/usr/bin/firefox");

//Fire Up Firefox Browser
WD  =   new FirefoxDriver();

//Navigate to Target Site
WD.get("https://MyFakeSite.org");

//Find first link / page to navigate to from site
WE   =  WD.findElement(By.xpath("/html/body/form/div[6]/div[1]/div/div[2]/div/div[2]/a"));
WD.get(WE.getAttribute("href"));

//Find second link / page to navigate to from current page
WE   = WD.findElement(By.xpath("/html/body/form/div[5]/div/div[2]/div[3]/div/span[2]/div[4]/a"));
WE.click();

//Train falls off the rails here:
try
{
WebDriverWait   wait;
wait   =   new WebDriverWait(WD,Duration.ofSeconds(30));

//Error occurs with this line
wait.until(ExpectedConditions.elementToBeClickable(WD.findElement(By.xpath("//*[@id=\"ctl00_Content1_button_accept\"]"))));

WE = WD.findElement(By.xpath("//[@id='ctl00_Content1_button_accept']")); 
//Never Reach this line - Error occurs and jumps to Catch...
WE.click(); 

}

Selenium can NOT find the element and results in an error: Unable to locate element: //[@id="ctl00_Content1_button_accept"]. Since Selenium can not find the element. I originally believe the code was erroring out on the following executable line from the error line above, but stand corrected. user @pcalkins attempted to help me suggesting I was on the right path - @pacalkins suggested an xpath w/ an "a" instead of the "*" - I tried that it did not work; same result / error message. An inspection of the html code using dev tools shows this:

<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>

Research into the network payload has been discouraged as the form should be automatically sending the payload / variables / necessary parameters if I can just get the "Accept" button to click. I've tried WebDriver, WebElement, JavascriptExecutor, send keys (with enter key - performing the manual task of hitting the enter key on the keyboard works / advances you, but not when you send the keystrokes programmatically).

What do I do ? What is the code that will work ? Who can help me ? And a big thanks to @pcalkins for trying to help me out !!!

1

There are 1 best solutions below

0
On BEST ANSWER

So after months of trial and error, I stumbled upon this YouTube Video:

https://www.youtube.com/watch?v=_pZjz050TBA

In this video Prakash Narkhede walks through the stepping of xpath layers to arrive at the specific element he is searching for. My first challenge was understanding where in the DevTools he was entering the search criteria to evaluate the xpath - this was simply f to 'find' the elements in the DOM (simple but was confusing at the time).

Next I navigated as he did entering each node down to where I was trying to find the element I needed to click. The following xpath:

"//div[@class='deptparagraphdescription2']/p[@class='center_align']/a"

finally got click to work w/ the following code (Note: Challenging page url found in code below as well:

public static void Main() {
    WebElement WE;
    WebDriver WD;

    System.setProperty("webdriver.gecko.driver", "/home/ms/Development/WS/WebScrape/src-fl/geckodriver/geckodriver");
    System.setProperty("webdirver.firfox.bin", "/usr/bin/firefox");

    // Fire Up Firefox Browser
    WD = new FirefoxDriver();

    // Navigate to Target Site / specific page where object can NOT be clicked ('Accept button')
    L.SendLog("Running", "Testing", "JETesting", "Main", "Navigating to problem page", "");
    WD.get("https://app02.clerk.org/cm_rpt/?id=fci");
    L.SendLog("Running", "Testing", "JETesting", "Main", "Navigation to problem page completed", "");

    try {
        L.SendLog("Running", "Testing", "JETesting", "Main", "Attempting to select and click element by using xpath '//div[@class='deptparagraphdescription2']/p[@class='center_align']/a'", "");
        WebDriverWait wait;
        wait = new WebDriverWait(WD, Duration.ofSeconds(30));

        WE = wait.until(ExpectedConditions.elementToBeClickable(WD.findElement(By.xpath("//div[@class='deptparagraphdescription2']/p[@class='center_align']/a"))));
        WE.click();
        s = "pause for breakpoint";
    } catch (Exception e) {
        e.printStackTrace();
        L.SendLog("Running", "Testing", "JETesting", "Main", "Clicking by xpath '//div[@class='deptparagraphdescription2']/p[@class='center_align']/a' FAILED", e.getMessage());
        s = "pause for breakpoint";
    }
}

Traversing the path as Prakash did in his video, helped ID an xpath for the anchor () element to allow the click to work. Simply copying the xpath for what I believed was the element to select and click on didn't work. Selecting an xpath via the traversal path technique seemed to solve the problem.