Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/398.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Selenium StaleElementReferenceFrameToBeavailable和SwitchToIt之后出现异常_Java_Selenium_Staleelementreferenceexception_Xpath - Fatal编程技术网

Java Selenium StaleElementReferenceFrameToBeavailable和SwitchToIt之后出现异常

Java Selenium StaleElementReferenceFrameToBeavailable和SwitchToIt之后出现异常,java,selenium,staleelementreferenceexception,xpath,Java,Selenium,Staleelementreferenceexception,Xpath,如果Selenium抛出StaleElementReferenceException,通常是(?),因为由于外部干扰,Selenium“拥有”的网页(DOM)状态已更改,但我在switch语句之前添加frameToBeavailable和switchToIt时开始得到它。我的代码: driver.findElement(By.xpath(xpDownArrow)).click(); WebElement iframeElement = driver.findElement(By.xpath(xp

如果Selenium抛出StaleElementReferenceException,通常是(?),因为由于外部干扰,Selenium“拥有”的网页(DOM)状态已更改,但我在
switch
语句之前添加
frameToBeavailable和switchToIt
时开始得到它。我的代码:

driver.findElement(By.xpath(xpDownArrow)).click();
WebElement iframeElement = driver.findElement(By.xpath(xpIframe));
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(xpIframe)));
driver.switchTo().frame(iframeElement);
最初,我的代码只包含第1、2和4行,它们大部分时间都工作,但执行失败了10-20%,因此我添加了第2行,但现在每次执行此代码时,我都会得到
StaleElementReferenceException
。我不明白。为什么第3行会更改DOM?这不是建议对第3行执行
开关的方法吗

wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath(xpIframe)));

您已经切换到iframe。所以不需要第4行-它只是尝试切换到同一帧。尝试删除它(第2行也是冗余的)

而不是那些较小的冗余步骤,您可以使用以下最佳方式切换到所需的

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("xpIframe")));

参考文献 您可以在以下内容中找到一些相关讨论: