Robot Framework-Selenium Webdriver-Java:调用全局变量时出现陈旧元素引用异常

Robot Framework-Selenium Webdriver-Java:调用全局变量时出现陈旧元素引用异常,java,selenium-webdriver,robotframework,Java,Selenium Webdriver,Robotframework,Robot Framework-Selenium Webdriver-Java:有人告诉我为什么在函数中调用全局变量时会出现过时的元素引用异常 我创建了下面的java方法,并在Robot框架中调用了这个关键字 public String CreateOpportunity() { String OpportunityName = "Optimum Wartung"+RandomNumber(); WaitAndClickElement("id",SalesforceOpportu

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);
    }
}
更正代码:

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;
    }
}
}
public void beginasesss(字符串opportunity str){
//opportunityString=CreateOpportunity(opportunityStr);
List opportunities=driver.findElements(By.xpath(//a[contains(@id,'offer-item')]);
System.out.println(“进入开始评估功能”+opportunityStr);
for(WebElement机会主义:机遇)
{
WebElement textele=Opportunity.findElement(By.cssSelector(“div.offer-name.no contracts”);
字符串textval=textele.getText();
System.out.println(“TextValue为:+textval”);
if(textval.equalsIgnoreCase(opportunityStr))
{
WaitTillElement可选择(“cssSelector”,CustomerPageData.OpportunityStatus);
opportunite.findElement(由.cssSelector(“div.opportunity-status”))。单击();
System.out.println(“点击的”);
打破
}
}
}

如果您获得一个元素,然后在页面刷新后尝试使用该元素,您将获得一个过时的元素异常。即使刷新的结果是完全相同的页面,所有元素都将变得“过时”

如果您获得一个元素,然后在页面刷新后尝试使用该元素,则会出现一个陈旧的元素异常。即使刷新的结果是完全相同的页面,所有元素都将变得“过时”

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

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

当使用不同的浏览器,如Chrome、Firefox,IE时,是否会出现相同的错误?在一个测试用例中设置变量,然后,下面的测试用例将您记录到一些内容中。您确定没有从设置变量到使用变量的位置进行页面刷新吗?我猜,在这种方法中-BeginAsEss,单击操作后打破循环。如果需要,请告诉我works@Helio-不幸的是,我的网页只支持chrome。我们已在其他浏览器中阻止了该页面。@Bryan-是,有页面刷新。更重要的是,变量在一个环境中设置,并在另一个环境中使用该值。例如,这里我在登录Salesforce之后创建了这个变量(注意关键字“Logon to Salesforce”)。一旦我创建Im登录到KCC(注意关键字“Logon To KCC”),其中Im使用了该值。当使用不同的浏览器(如Chrome、Firefox、IE)时,是否会出现相同的错误?您在一个测试用例中设置了变量,然后下面的测试用例将您登录到某个内容中。您确定没有从设置变量到使用变量的位置进行页面刷新吗?我猜,在这种方法中-BeginAsEss,单击操作后打破循环。如果需要,请告诉我works@Helio-不幸的是,我的网页只支持chrome。我们已在其他浏览器中阻止了该页面。@Bryan-是,有页面刷新。更重要的是,变量在一个环境中设置,并在另一个环境中使用该值。例如,这里我在登录Salesforce之后创建了这个变量(注意关键字“Logon to Salesforce”)。一旦我创建Im登录到KCC(注意关键字“Logon To KCC”),Im在其中使用值。
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;
    }
}
}