[英]Selenium switchTo return error org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
I have scenario to click hyperlink, and will open new tab (active window will move to new tab after click hyperlink) 我有点击超链接的方案,并将打开新选项卡(活动窗口将点击超链接后移动到新选项卡)
When i try to move to webdriver to new tab using switchTo()
method, then followed by WebDriverWait.until
browser automatically close with error 当我尝试使用
switchTo()
方法将webdriver移动到新选项卡时,然后WebDriverWait.until
浏览器自动关闭并显示错误
org.openqa.selenium.WebDriverException: unknown error: cannot determine loading status
from no such execution context
(Session info: chrome=73.0.3683.103)
I use System.out.println(driver.getWindowHandle())
and i can see driver moving to new tab. 我使用
System.out.println(driver.getWindowHandle())
,我可以看到驱动程序移动到新选项卡。
How i can fix above error? 我如何修复上述错误? I have try using Iterator for loop into windowHandle
我尝试使用Iterator循环到windowHandle
Seems can't use WebDriverWait.until(ExpectedConditions) to wait for new tab. 似乎无法使用WebDriverWait.until(ExpectedConditions)等待新选项卡。 Always getting an error
cannot determine loading status from no such execution context
始终收到错误
cannot determine loading status from no such execution context
Weird thing, I can use Thread.sleep(1000)
. 奇怪的是,我可以使用
Thread.sleep(1000)
。
How i can avoid using Thread.sleep in this case? 在这种情况下我怎么能避免使用Thread.sleep? Because implicit wait can't work too
因为隐式等待也不行
Working code with Thread.sleep() 使用Thread.sleep()工作代码
public class MyCode {
private WebDriver driver;
private WebDriverWait wait;
@Test
public void openPrestaShopFromDemoWebsite() {
System.setProperty("webdriver.chrome.driver", "chromedriver");
ChromeOptions chromeOptions = new ChromeOptions()
.addArguments("--start-maximized", "--incognito");
driver = new ChromeDriver(chromeOptions);
driver.navigate().to("http://demo.prestashop.com");
wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingMessage")));
driver.switchTo().frame("framelive");
String parentTab = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Ecommerce software by PrestaShop")).click();
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> it = windowHandles.iterator();
while (it.hasNext()) {
String newTab = it.next();
if (!parentTab.equals(newTab)) {
driver.switchTo().window(newTab);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
wait.until(ExpectedConditions.titleIs("Create and develop your business with PrestaShop"));
driver.close();
}
}
driver.switchTo().window(parentTab);
driver.switchTo().frame("framelive");
assertThat(driver.findElement(By.linkText("Personal info")).isDisplayed());
driver.quit();
}
}
Not working code (cannot determine loading status from no such execution context) 不工作的代码(不能从没有这样的执行上下文确定加载状态)
public class MyCode {
private WebDriver driver;
private WebDriverWait wait;
private WebElement element;
@Test
public void openPrestaShopFromDemoWebsite() {
System.setProperty("webdriver.chrome.driver", "chromedriver");
ChromeOptions chromeOptions = new ChromeOptions()
.addArguments("--start-maximized", "--incognito");
driver = new ChromeDriver(chromeOptions);
driver.navigate().to("http://demo.prestashop.com");
wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.id("loadingMessage")));
driver.switchTo().frame("framelive");
String parentTab = driver.getWindowHandle();
driver.findElement(By.partialLinkText("Ecommerce software by PrestaShop")).click();
Set<String> windowHandles = driver.getWindowHandles();
Iterator<String> it = windowHandles.iterator();
while (it.hasNext()) {
String newTab = it.next();
if (!parentTab.equals(newTab)) {
driver.switchTo().window(newTab);
wait.until(ExpectedConditions.titleIs("Create and develop your business with PrestaShop"));
driver.close();
}
}
driver.switchTo().window(parentTab);
driver.switchTo().frame("framelive");
assertThat(driver.findElement(By.linkText("Personal info")).isDisplayed());
driver.quit();
}
}
I had been running into this same problem for a few days and it seems that it is actually an issue with chromedriver. 我已经遇到了这个问题几天,似乎它实际上是chromedriver的一个问题。 The solution that worked for me was to add the following flag/argument to your chromedriver options:
对我有用的解决方案是在chromedriver选项中添加以下标志/参数:
--disable-site-isolation-trials
Here is the chromedriver issue (and solution) I'm referring to. 这是我所指的chromedriver问题(和解决方案)。
If the flag does not work, you may also try upgrading to chromedriver v75 如果该标志不起作用,您也可以尝试升级到chromedriver v75
As the link is opening in a new tab, you need to switch the driver to the new tab and for that, you can use the following code: 当链接在新选项卡中打开时,您需要将驱动程序切换到新选项卡,为此,您可以使用以下代码:
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs.get(1));
And if you want to switch to the original tab, you need to use: 如果要切换到原始选项卡,则需要使用:
driver.switchTo().window(tabs.get(0));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.