【问题标题】:Robot Framework - Selenium Webdriver - Java: stale element reference exception when calling a global variable机器人框架 - Selenium Webdriver - Java:调用全局变量时过时的元素引用异常
【发布时间】:2017-10-19 01:56:20
【问题描述】:

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

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

更正的代码:

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

【问题讨论】:

  • 在使用不同的浏览器(如 Chrome、Firefox、IE)时是否会出现同样的错误?
  • 您在一个测试用例中设置变量,然后下面的测试用例将您记录到某事中。您确定没有从您将变量设置到使用它的位置刷新页面吗?
  • 我猜,在这个方法中 - BeginAssess,点击动作后会打破循环。让我知道它是否有效
  • @Helio - 不幸的是我的网页只支持 chrome。我们已经在其他浏览器中屏蔽了该页面。
  • @Bryan - 是的,有一个页面刷新。更重要的是,变量是在一个环境中设置的,并在其他环境中使用该值。例如,在这里我在登录到 Salesforce 后创建了这个变量(注意关键字“登录到 Salesforce”)。一旦我创建 Im 登录到 KCC(注意关键字“Logon To KCC”),我在其中使用该值。

标签: java selenium-webdriver robotframework


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    那么解决方案是睡眠直到页面被刷新。

    【讨论】:

      猜你喜欢
      • 2018-05-11
      • 1970-01-01
      • 2017-12-24
      • 2017-09-26
      • 1970-01-01
      • 2018-04-01
      • 2020-08-04
      • 1970-01-01
      • 2018-04-10
      相关资源
      最近更新 更多