Java 用于定位IFRAM的回路,并切换到它不工作
我用java代码为iframe定位器做了一个循环 但它不起作用。有人看到问题了吗 使用以下命令调用类:Java 用于定位IFRAM的回路,并切换到它不工作,java,selenium,Java,Selenium,我用java代码为iframe定位器做了一个循环 但它不起作用。有人看到问题了吗 使用以下命令调用类: driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver,driver.findElement(By.cssSelector("[href*='Searc
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
WebElement searchButton = IFrameLocator.switchToIFrameWithElement(driver,driver.findElement(By.cssSelector("[href*='Search.mvc'][class*='magnify']")));
and use this after:
searchButton.click();
public class IFrameLocator {
public static WebElement switchToIFrameWithElement(WebDriver driver, WebElement element) {
try {
driver.switchTo().defaultContent();
element.isDisplayed();
} catch (Exception continueFlow) {
WebDriverWait wait = new WebDriverWait(driver, 20);
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
try {
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(frame));
if (element.isDisplayed()) {
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} return element;
}
}
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
WebElement searchButton=IFrameLocator.SwitchToFrameWithElement(驱动程序,驱动程序.findElement(由.cssSelector(“[href*='Search.mvc'][class*='magnize']));
并在以下情况下使用:
搜索按钮。单击();
公共类IFrameLocator{
公共静态WebElement切换到FrameWithElement(WebDriver驱动程序,WebElement元素){
试一试{
driver.switchTo().defaultContent();
元素。isDisplayed();
}catch(异常continueFlow){
WebDriverWait wait=新的WebDriverWait(驱动程序,20);
列表帧=driver.findElements(由.cssSelector(“iframe”);
for(WebElement框架:框架){
driver.switchTo().defaultContent();
试一试{
等待.直到(预期条件.frameToBeavailable并切换到it(frame));
if(element.isDisplayed()){
打破
}
}catch(忽略NoTouchElementException | StaleElementReferenceException | ElementNotInteractiableException){
}
}
}返回元素;
}
}
因为我是通过cssSelector传递整个Webelement,所以它甚至没有进入循环。所以我像这样传递元素:
WebElement searchButton=IFrameLocator.SwitchToFrameWithElement(驱动程序,由.cssSelector(“[href*='Search.mvc']][class*='magnize']”)
因此,我更改了代码,删除了2个点中的By.cssSelector,现在一切正常:
公共类IFrameLocator{
public static WebElement switchToIFrameWithElement(WebDriver driver, By element) {
driver.switchTo().defaultContent();
try {
if (driver.findElement(element).isDisplayed()) ;
{
System.out.println("Element is displayed on main page");
}
} catch (Exception continueFlow) {
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
System.out.println("going back to main page");
try {
driver.switchTo().frame(frame);
System.out.println("switched to next frame: " + frame);
if (driver.findElement(element).isDisplayed()) {
System.out.println("element is found in frame: " + frame);
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} System.out.println("returned element succesfully");
return driver.findElement(element);
}
公共静态WebElement切换到FrameWithElement(WebDriver驱动程序,按元素){
driver.switchTo().defaultContent();
试一试{
if(driver.findElement(element.isDisplayed());
{
System.out.println(“元素显示在主页面上”);
}
}catch(异常continueFlow){
列表帧=driver.findElements(由.cssSelector(“iframe”);
for(WebElement框架:框架){
driver.switchTo().defaultContent();
System.out.println(“返回主页”);
试一试{
驱动程序.切换到().帧(帧);
System.out.println(“切换到下一帧:“+frame”);
if(driver.findElement(element.isDisplayed()){
System.out.println(“在帧:“+frame”中找到元素);
打破
}
}catch(忽略NoTouchElementException | StaleElementReferenceException | ElementNotInteractiableException){
}
}
}System.out.println(“成功返回元素”);
返回驱动程序findElement(元素);
}
}因为我是通过cssSelector传递整个Webelement,所以它甚至没有进入循环。所以我像这样传递元素: WebElement searchButton=IFrameLocator.SwitchToFrameWithElement(驱动程序,由.cssSelector(“[href*='Search.mvc']][class*='magnize']”) 因此,我更改了代码,删除了2个点中的By.cssSelector,现在一切正常: 公共类IFrameLocator{
public static WebElement switchToIFrameWithElement(WebDriver driver, By element) {
driver.switchTo().defaultContent();
try {
if (driver.findElement(element).isDisplayed()) ;
{
System.out.println("Element is displayed on main page");
}
} catch (Exception continueFlow) {
List<WebElement> frames = driver.findElements(By.cssSelector("iframe"));
for (WebElement frame : frames) {
driver.switchTo().defaultContent();
System.out.println("going back to main page");
try {
driver.switchTo().frame(frame);
System.out.println("switched to next frame: " + frame);
if (driver.findElement(element).isDisplayed()) {
System.out.println("element is found in frame: " + frame);
break;
}
} catch (NoSuchElementException | StaleElementReferenceException | ElementNotInteractableException ignored) {
}
}
} System.out.println("returned element succesfully");
return driver.findElement(element);
}
公共静态WebElement切换到FrameWithElement(WebDriver驱动程序,按元素){
driver.switchTo().defaultContent();
试一试{
if(driver.findElement(element.isDisplayed());
{
System.out.println(“元素显示在主页面上”);
}
}catch(异常continueFlow){
列表帧=driver.findElements(由.cssSelector(“iframe”);
for(WebElement框架:框架){
driver.switchTo().defaultContent();
System.out.println(“返回主页”);
试一试{
驱动程序.切换到().帧(帧);
System.out.println(“切换到下一帧:“+frame”);
if(driver.findElement(element.isDisplayed()){
System.out.println(“在帧:“+frame”中找到元素);
打破
}
}catch(忽略NoTouchElementException | StaleElementReferenceException | ElementNotInteractiableException){
}
}
}System.out.println(“成功返回元素”);
返回驱动程序findElement(元素);
}
}“FrameToBeAvailable和SwitchToIt”采用定位器。也许可以检查一下它是否存在。。。然后在其上运行switchto:driver.switchto().frame([name或id]);switchTo().frame()也可以通过使用元素对象来工作;但它甚至不像看上去的那样使用这种方法。驱动程序没有加载。。。没有这样的元素:无法定位元素:{“方法”:“css选择器”,“选择器”:“[href*='Search.mvc'][class*='magnific']]”然后删除隐式等待。“FrameToBeAvailable和SwitchToIt”接受定位器。也许可以检查一下它是否存在。。。然后在其上运行switchto:driver.switchto().frame([name或id]);switchTo().frame()也可以通过使用元素对象来工作;但它甚至不像看上去的那样使用这种方法。驱动程序没有加载。。。没有这样的元素:无法找到元素:{“方法”:“css选择器”,“选择器”:“[href*='Search.mvc'][class*='magnific']]”然后摆脱隐式等待。