stale element reference: element is not attached to the page document

来源:互联网 发布:微信for windows和uwp 编辑:程序博客网 时间:2024/05/20 11:24

报错原因:

由于页面刷新后,需要重新定位元素

解决方案:try-catch

报错前的代码:

WebElement element=driver.findElement(By.xpath("//input[@id='name']"));            element.sendKeys("输入的文本");

解决后的代码:

try {            WebElement element=driver.findElement(By.xpath("//input[@id='name']"));            element.sendKeys("输入的文本");        }        catch(org.openqa.selenium.StaleElementReferenceException ex)        {            WebElement element=driver.findElement(By.xpath("//input[@id='name']"));            element.sendKeys("输入的文本");        }
注意:catch()内和常见的区别,这里的是:org.openqa.selenium.StaleElementReferenceException ex

总结:

1.try-catch相当于在页面刷新后再次获取一次元素,即在try中判断找不到元素时,执行catch中代码再次加载一次元素2.注意catch括号中的代码,和平常的catch不太一样
阅读全文
0 0
原创粉丝点击