繁体   English   中英

机器人框架-Selenium Webdriver-Java:调用全局变量时,陈旧元素引用异常

[英]Robot Framework - Selenium Webdriver - Java: stale element reference exception when calling a global variable

机器人框架-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}并将其设置为Global变量,如下所示。 并将此变量传递给名为“ 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;
    }
}
}

如果获取元素,则会得到一个过时的元素异常,然后在页面刷新后尝试使用该元素。 即使刷新导致页面完全相同,所有元素也会变为“陈旧”。

然后的解决方案是休眠,直到刷新页面。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM