2017-05-19 19 views
0

Robot Framework - Selenium Webdriver - Java:有人让我知道为什么当调用全局变量到我的函数时,我得到陈旧的元素引用异常。机器人框架 - Selenium Webdriver - Java:调用全局变量时的陈旧元素引用异常

我已经创建了下面的java方法,并在Robot框架中调用了这个关键字。

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); 
    } 
} 

以下是RF代码。我创建了一个变量$ {Oppo}并将其设置为全局变量,如下所示。并将该变量传递给名为“Begin Assess”的关键字。它执行代码,但以陈旧的元素异常结束。我已经把等待条件,但它仍然有相同的情况。帮助我,我要去哪里错了。 注意:我没有使用selenium2library。只使用硒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} 

更正代码:

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; 
    } 
} 
} 
+1

当使用Chrome,Firefox,IE等不同浏览器时,您是否得到相同的错误? – Helio

+1

您可以在一个测试用例中设置变量,然后下面的测试用例将您记录到某个东西中。你确定从你设置变量的地方没有页面刷新到你使用它的地方吗? –

+1

我想,在这种方法 - BeginAssess,点击行动后打破循环。让我知道它是否工作 – Karthikeya

回答

2

你会得到一个陈旧的元素异常,如果你得到一个元素,然后尝试刷新页面后使用的元素。即使刷新结果在完全相同的页面中,所有元素都将变得“陈旧”。

0

解决方案然后,将睡眠直到页面刷新。

相关问题