0

Robot Framework - Selenium Webdriver - Java: Somebody let me know why i get stale element reference exception when calling a global variable into my function.

I have created the below java method and called this keyword in Robot framework.

public String CreateOpportunity()
{
    String OpportunityName = "Optimum Wartung"+RandomNumber();
    WaitAndClickElement("id",SalesforceOpportunityPageData.OpportunityTab);
    ClickOnElement("cssSelector", SalesforceOpportunityPageData.NewButton);
    SelectDropDownValues ("id",SalesforceOpportunityPageData.SiteCountryField,SalesforceOpportunityPageData.SiteCountryValue);
EnterValues("id",SalesforceOpportunityPageData.ProjectEndDateField,SalesforceOpportunityPageData.ProjectEndDateValue);
    ClickOnElement("cssSelector",SalesforceOpportunityPageData.SaveButton);
    return OpportunityName;
}
public void BeginAssess(String opportunityStr){
//opportunityString=CreateOpportunity(opportunityStr);
List<WebElement> opportunities = driver.findElements(By.xpath("//a[contains(@id,'offer-item')]"));
System.out.println("Entered into Begin Asses function "+ opportunityStr);

for(WebElement opportunite:opportunities)
{
    WebElement textele = opportunite.findElement(By.cssSelector("div.offer-name.no-contracts"));
    String textval = textele.getText();
    System.out.println("TextValue is: " + textval);
    if(textval.equalsIgnoreCase(opportunityStr))
    {
        WaitTillElementToBeClickable("cssSelector",CustomerPageData.OpportunityStatus);
        opportunite.findElement(By.cssSelector("div.opportunity-status")).click();
        System.out.println("Its clicked2");
    }
}
}   
public void WaitTillElementToBeClickable(String locatorType,String locatorVaue)
{
    try{
    WebDriverWait wait = new WebDriverWait(driver,200);

    if(locatorType.equalsIgnoreCase("cssSelector"))
        wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(locatorVaue)));

    else if(locatorType.equalsIgnoreCase("xpath"))
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath(locatorVaue)));

    else if(locatorType.equalsIgnoreCase("id"))
        wait.until(ExpectedConditions.elementToBeClickable(By.id(locatorVaue)));
    }
    catch(Exception e){
        System.out.println("Webdriver Locator Error"+e);
    }
}

The below is the RF code. I have created a Variable ${Oppo} and set this as Global variable as like below. And passing this variable into the keyword called "Begin Assess". It executes the code, but ending up with stale element exception. I have put the wait condition, but still it has the same situation. Help me where I'm going wrong. Note: I'm not using selenium2library. Using only selenium webdriver.

*** Variable ***
${Oppo}
*** Test Cases ***
Create Opportunities in Salesforce Environment
  Logon To Salesforce
  ${Oppo}=  Create Opportunity
  Set Global Variable  ${Oppo}
Logon To KCC With Valid Credentials
  Logon To KCC
Verify the Salesforce Data is synchronized with KCC tool
  Update KCC Data
Complete The Assessment For An Opportunity
  Search Customer Account  Automation
  Expand Customer Account
  Begin Assess  ${Oppo}

Corrected Code:

public void BeginAssess(String opportunityStr){
//opportunityString=CreateOpportunity(opportunityStr);
List<WebElement> opportunities = driver.findElements(By.xpath("//a[contains(@id,'offer-item')]"));
System.out.println("Entered into Begin Asses function "+ opportunityStr);

for(WebElement opportunite:opportunities)
{
    WebElement textele = opportunite.findElement(By.cssSelector("div.offer-name.no-contracts"));
    String textval = textele.getText();
    System.out.println("TextValue is: " + textval);
    if(textval.equalsIgnoreCase(opportunityStr))
    {
        WaitTillElementToBeClickable("cssSelector",CustomerPageData.OpportunityStatus);
        opportunite.findElement(By.cssSelector("div.opportunity-status")).click();
        System.out.println("Its clicked");
        break;
    }
}
}
7
  • 1
    Do you get the same error, when using different browser, like Chrome, Firefox, IE? Commented May 19, 2017 at 9:41
  • 1
    You set the variable in one test case, and then the following test case logs you into something. Are you certain there are no page refreshes from where you set the variable to where you use it? Commented May 19, 2017 at 11:25
  • 1
    I guess, in this method - BeginAssess, after click action break the loop. Let me know if it works Commented May 20, 2017 at 6:34
  • @Helio - Unfortunately my webpage supports only chrome. We have blocked the page in other browsers. Commented May 20, 2017 at 7:23
  • @Bryan - Yes there is a page refresh. More over and the important thing is that the variable is set in one environment and using the value in other environment. For eg., Here I create this variable after Logging into Salesforce(Note the keyword "Logon to Salesforce"). Once I create Im logging into KCC(Note the keyword "Logon To KCC") in which im using the value. Commented May 20, 2017 at 7:29

2 Answers 2

2

You will get a stale element exception if you get an element, and then try to use the element after a page refresh. Even if the refresh results in the exact same page, all elements will become "stale".

Sign up to request clarification or add additional context in comments.

Comments

0

The solution then, would be to sleep until the page is refreshed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.