我正在尝试自动化“http://zzzscore.com/dontap/en ”并提出了以下代码
自动化在一段时间内表现良好,但后来失败并出现以下错误
Exception in thread "main" org.openqa.selenium.StaleElementReferenceException: stale element reference: element is not attached to the page document (Session info: chrome=81.0.4044.122)
driver.get("http://zzzscore.com/dontap/en/");
WebElement Parent = driver.findElement(By.id("grid"));
WebElement Child = Parent.findElement(By.xpath("//*[@id='grid']/div[1]"));
Child.findElement(By.xpath("//*[contains(@class, 'tab b start')]")).click();
System.out.println("Done 1");
for (int i = 2; i <= 4; i++) {
Child.findElement(By.xpath("//*[contains(@class, 'tab b')]")).click();
System.out.println("Done " + "" + i);
}
for ( int i = 5; i<15; i++) {
if ( driver.findElement(By.xpath("//*[contains(@class, 'tab b')]")).isDisplayed() ) {
Thread.sleep(500);
driver.findElement(By.xpath("//*[contains(@class, 'tab b')]")).click();
System.out.println("Done "+ i);
}
else System.out.println("Element is not present");
}
最佳答案
游戏中只有 5 行在固定的时间间隔后每次都会重复。下面的代码工作得很好,只需要改变循环中的时间和迭代计数。
System.setProperty("webdriver.chrome.driver", "C:\\Users\\WaveRiders\\Downloads\\qa_automation_christies_bibg-master\\driver\\chromedriver.exe");
// Instantiate a ChromeDriver class.
WebDriver driver=new ChromeDriver();
// Launch Website
driver.navigate().to("http://zzzscore.com/dontap/en");
//Maximize the browser
driver.manage().window().maximize();
int i = 1;
while(i < 50) {
driver.findElement(By.xpath("//div[@class='grid x4']/div[1]/div[contains(@class,'tab b')]")).click();
Thread.sleep(200);
System.out.println("Clicked element : "+1);
driver.findElement(By.xpath("//div[@class='grid x4']/div[2]/div[contains(@class,'tab b')]")).click();
Thread.sleep(200);
System.out.println("Clicked element : "+2);
driver.findElement(By.xpath("//div[@class='grid x4']/div[3]/div[contains(@class,'tab b')]")).click();
Thread.sleep(200);
System.out.println("Clicked element : "+3);
driver.findElement(By.xpath("//div[@class='grid x4']/div[4]/div[contains(@class,'tab b')]")).click();
Thread.sleep(200);
System.out.println("Clicked element : "+4);
driver.findElement(By.xpath("//div[@class='grid x4']/div[5]/div[contains(@class,'tab b')]")).click();
Thread.sleep(200);
System.out.println("Clicked element : "+5);
//Change this wait time
Thread.sleep(500);
i++;
}
关于java - Selenium 在线游戏自动化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61424129/