gpt4 book ai didi

java - 机器人框架 - Selenium Webdriver - Java : stale element reference exception when calling a global variable

转载 作者:行者123 更新时间:2023-12-02 02:49:31 24 4
gpt4 key购买 nike

机器人框架 - 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。仅使用 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;
}
}
}

最佳答案

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

关于java - 机器人框架 - Selenium Webdriver - Java : stale element reference exception when calling a global variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44061524/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com