机器人框架 - Selenium Webdriver语言 - Java:调用全局变量时出现过时的元素引用异常



机器人框架 - Selenium Webdriver - Java:有人让我知道为什么在我的函数中调用全局变量时会出现过时的元素引用异常。

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

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} 并将其设置为全局变量,如下所示。并将此变量传递到名为"开始评估"的关键字中。它执行代码,但最终出现过时的元素异常。我已经提出了等待条件,但它仍然具有相同的情况。在我出错的地方帮助我。注意:我没有使用selenium2library。仅使用硒网络驱动程序。

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

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

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

最新更新