在我的应用程序中,所有选项卡都是一个框架下的类别。我用过:
driver.switchTo().frame((WebElement) By.name("contents"));
System.out.println("Frame is selected");
验证框架是否存在,但出现以下异常:
>Exception in thread "main" java.lang.ClassCastException:
org.openqa.selenium.By$ByName cannot be cast to org.openqa.selenium.WebElement
at selenium_test.iebrowser.main(iebrowser.java:75)
最佳答案
您的错误表明您正在尝试将 By
从 By.name("contents")
转换为 WebElement
your(WebElement)
...这当然是无意义的!
根据Selenium API , .switchTo().frame()
具有以下签名之一:
WebDriver frame(int index);
WebDriver frame(String nameOrId);
WebDriver frame(WebElement frameElement);
您需要的是以下之一:
driver.switchTo().frame("contents"); // uses the nameOrId
driver.switchTo().frame(driver.findElement(By.name("contents"))); // uses the frameElement
关于java - 如何判断框架是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29928221/