2
votes

Environment: Eclipse - Java - Junit - firefox webdriver

Screen shot url : https://www.dropbox.com/s/26fl2l1y1mkpxiz/Screenshot%202014-01-06%2001.52.43.png

I am trying to test a webpage but unable to find element (looks like a button). Firebug xpath is :.//*[@id='DERIVED_REGFRM1_LINK_ADD_ENRL$118$']

With the same Xpath, it works correctly with selenium IDE (By recording).

It's html code is:

<td valign="top" align="left">
    <div id="win0divDERIVED_REGFRM1_LINK_ADD_ENRL$118$">
        <span class="SSSBUTTON_CONFIRMLINK" title="Enroll in Classes">
            <a id="DERIVED_REGFRM1_LINK_ADD_ENRL$118$" class="SSSBUTTON_CONFIRMLINK" 
            href="javascript:submitAction_win0(document.win0,
    'DERIVED_REGFRM1_LINK_ADD_ENRL$118$');" 
    tabindex="192" ptlinktgt="pt_peoplecode" 
    name="DERIVED_REGFRM1_LINK_ADD_ENRL$118$">Proceed to Step 2 of 3</a>
        </span>
    </div>
</td>

I have tried my finding By.xpath, id, etc. eg: driver.findElement(By.xpath("//*[@id='DERIVED_REGFRM1_LINK_ADD_ENRL$118$'] ")).click();

What is the correct solution?

6
Is this a public page? if so could you please point to it? Could you please add the error (stacktrace) to your question? - Ittiel
It could be that selenium tries to find element before the element is present on the page. Did you try to add wait? - Ievgen
I have some doubts about using id. Are you sure id is always the same and doesn't changes dynamically (e.g. like in ExtJS applications)? - olyv
With the same Xpath, it works correctly with selenium IDE (By recording). @Ittiel: it's a private page, stack trace shows element not found error. - user2924175
@Evgeniy: I used thread.sleep(20000) but still it is not working - user2924175

6 Answers

2
votes

Try using CSS

WebElement element = new WebDriverWait(driver,30).until(ExpectedConditions.elementToBeClickable(By.cssSelector("a[id*='DERIVED_REGFRM1_LINK_ADD_ENRL'][class='SSSBUTTON_CONFIRMLINK']")));
2
votes

It's possible the page object has not loaded at the time Webdriver is searching for it. Try to locate the step that is failing, and place an implicit wait/sleep before it.

If Webdriver is able to find the page object after you know it's a page load issue.

  1. Identify the step the test is failing on.
  2. Place a Wait/Sleep before the step ie... 20 seconds (way more than enough).
  3. Execute your test again.
  4. If the test now passes, Webdriver searched for the page object before it fully loaded.
0
votes

I think it can be because element you are trying to find is not present on the page at the moment driver searches for it. One of solution would be to define pageLoadTimeout. You can do that with such line of code:

driver.manage().timeouts().pageLoadTimeout(60, TimeUnit.SECONDS); 

You should increase the time if your page is loading longer. Second thing that I am using to ensure driver waits while page is loaded (especially when content is dynamic loaded by some scripts) is custom waitForPageLoaded function, which should be called after your webdriver opens specific page or clicks on button/link that does the page open action.

Example of the function:

public void waitForPageLoaded(WebDriver driver) {

 ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
    public Boolean apply(WebDriver driver) {
      return ((JavascriptExecutor)driver).executeScript("return document.readyState").equals("complete");
    }
  };

 Wait<WebDriver> wait = new WebDriverWait(driver,30);
  try {
          wait.until(expectation);
  } catch(Throwable error) {
          assertFalse("Timeout waiting for Page Load Request to complete.",true);
  }
}
0
votes

Hi, thanks for the help but i already used 60 sec thread.sleep and also used your way but it's not working. I tried to locate 5-6 elements on that page but it is also not working...i need it to be done in urgent. Error : unable to locate element

Although this is not the best approach, another possible solution would be to try using SeleniumIDE to manually record a script clicking that button.

If You're able to click that button on playback, just copy the block of text and implement it into your webdriver script. Given they're the exact same, your issue should be resolved.

0
votes

Copy the xpath from error given, and find it on inspect element of web portal you are testing, if the element is found then copy that particular element path from inspect element field and paste it on particular step on tool you are working on. eg; katalon, seleinum etc.

enter image description here

-1
votes

#

driver1.find_element_by_id("ContentPlaceHolder1_txtLifeAssuredName").send_keys("62221889")
        driver1.find_element_by_id("ContentPlaceHolder1_txtLifeAssuredName").clear()
        driver1.find_element_by_id("ContentPlaceHolder1_txtLifeAssuredName").send_keys("Bob Tan")