org.openqa.selenium.WebDriver.switchTo()方法的使用及代码示例

x33g5p2x  于2022-02-02 转载在 其他  
字(6.5k)|赞(0)|评价(0)|浏览(138)

本文整理了Java中org.openqa.selenium.WebDriver.switchTo()方法的一些代码示例,展示了WebDriver.switchTo()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。WebDriver.switchTo()方法的具体详情如下:
包路径:org.openqa.selenium.WebDriver
类名称:WebDriver
方法名:switchTo

WebDriver.switchTo介绍

[英]Send future commands to a different frame or window.
[中]将未来的命令发送到其他帧或窗口。

代码示例

代码示例来源:origin: selenide/selenide

public SelenideTargetLocator(Config config, WebDriver webDriver) {
 this.config = config;
 this.webDriver = webDriver;
 this.delegate = webDriver.switchTo();
}

代码示例来源:origin: stackoverflow.com

WebDriver driver = ... // initialize the driver
WebElement currentElement = driver.switchTo().activeElement();

代码示例来源:origin: selenide/selenide

@Override
public WebDriver apply(WebDriver driver) {
 try {
  return driver.switchTo().frame(driver.findElement(locator));
 } catch (NoSuchFrameException e) {
  return null;
 } catch (WebDriverException e) {
  return null;
 }
}

代码示例来源:origin: galenframework/galen

@Override
public void switchToParentFrame() {
  driver.switchTo().parentFrame();
}

代码示例来源:origin: selenide/selenide

@Override
public WebDriver apply(WebDriver driver) {
 try {
  List<String> windowHandles = new ArrayList<>(driver.getWindowHandles());
  return driver.switchTo().window(windowHandles.get(index));
 } catch (IndexOutOfBoundsException windowWithIndexNotFound) {
  return null;
 }
}

代码示例来源:origin: galenframework/galen

@Override
public void switchToFrame(PageElement mainObject) {
  WebPageElement webPageElement = (WebPageElement)mainObject;
  driver.switchTo().frame(webPageElement.getWebElement());
}

代码示例来源:origin: selenide/selenide

@Override
public WebDriver apply(WebDriver driver) {
 try {
  return driver.switchTo().window(nameOrHandleOrTitle);
 } catch (NoSuchWindowException windowWithNameOrHandleNotFound) {
  try {
   return windowByTitle(driver, nameOrHandleOrTitle);
  } catch (NoSuchWindowException e) {
   return null;
  }
 }
}

代码示例来源:origin: selenide/selenide

private void closeNewWindows(WebDriver webDriver, String currentWindowHandle, Set<String> currentWindows) {
 Set<String> windowHandles = webDriver.getWindowHandles();
 if (windowHandles.size() != currentWindows.size()) {
  Set<String> newWindows = new HashSet<>(windowHandles);
  newWindows.removeAll(currentWindows);
  log.info("File has been opened in a new window, let's close " + newWindows.size() + " new windows");
  for (String newWindow : newWindows) {
   log.info("  Let's close " + newWindow);
   try {
    webDriver.switchTo().window(newWindow);
    webDriver.close();
   }
   catch (NoSuchWindowException windowHasBeenClosedMeanwhile) {
    log.info("  Failed to close " + newWindow + ": " + Cleanup.of.webdriverExceptionMessage(windowHasBeenClosedMeanwhile));
   }
   catch (Exception e) {
    log.warning("  Failed to close " + newWindow + ": " + e);
   }
  }
  webDriver.switchTo().window(currentWindowHandle);
 }
}

代码示例来源:origin: stackoverflow.com

Alert javascriptAlert = myTestDriver.switchTo().alert();
Alert javascriptprompt = myTestDriver.switchTo().alert();
javascriptprompt.sendKeys("This is Selenium Training");
javascriptprompt = myTestDriver.switchTo().alert();
javascriptprompt = myTestDriver.switchTo().alert();
javascriptprompt = myTestDriver.switchTo().alert();
myTestDriver.findElement(By.xpath("//input[@value = 'confirm']")).click();
Alert javascriptconfirm = myTestDriver.switchTo().alert();
javascriptconfirm.accept();
javascriptconfirm = myTestDriver.switchTo().alert();
javascriptconfirm = myTestDriver.switchTo().alert();
javascriptconfirm = myTestDriver.switchTo().alert();

代码示例来源:origin: selenide/selenide

/**
 * Switch to window/tab by name/handle/title except some windows handles
 * @param title title of window/tab
 */
protected static WebDriver windowByTitle(WebDriver driver, String title) {
 Set<String> windowHandles = driver.getWindowHandles();
 for (String windowHandle : windowHandles) {
  driver.switchTo().window(windowHandle);
  if (title.equals(driver.getTitle())) {
   return driver;
  }
 }
 throw new NoSuchWindowException("Window with title not found: " + title);
}

代码示例来源:origin: selenide/selenide

if (retryIfAlert) {
 try {
  Alert alert = webdriver.switchTo().alert();
  log.severe(e + ": " + alert.getText());
  alert.accept();

代码示例来源:origin: TEAMMATES/teammates

/**
 * Closes the current browser window and switches back to the last window used previously.
 */
public void closeCurrentWindowAndSwitchToParentWindow() {
  driver.close();
  driver.switchTo().window(windowHandles.pop());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isRubricSubQuestionBoxFocused(int qnNumber, int subQnIndex) {
  WebElement subQnBox = getRubricSubQuestionBox(qnNumber, subQnIndex);
  return subQnBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isMcqWeightBoxFocused(int qnNumber, int choiceIndex) {
  WebElement weightBox = getMcqWeightBox(qnNumber, choiceIndex);
  return weightBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isMsqWeightBoxFocused(int qnNumber, int choiceIndex) {
  WebElement weightBox = getMsqWeightBox(qnNumber, choiceIndex);
  return weightBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isMsqOtherWeightBoxFocused(int qnNumber) {
  WebElement weightBox = getMsqOtherWeightBox(qnNumber);
  return weightBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isRubricWeightBoxFocused(int qnNumber, int subQnIndex, int choiceIndex) {
  WebElement weightBox = getRubricWeightBox(qnNumber, subQnIndex, choiceIndex);
  return weightBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

public boolean isMcqOtherWeightBoxFocused(int qnNumber) {
  WebElement weightBox = getMcqOtherWeightBox(qnNumber);
  return weightBox.equals(browser.driver.switchTo().activeElement());
}

代码示例来源:origin: TEAMMATES/teammates

/**
 * Switches to new browser window for browsing.
 */
public void switchToNewWindow() {
  String curWin = driver.getWindowHandle();
  for (String handle : driver.getWindowHandles()) {
    if (!handle.equals(curWin) && !windowHandles.contains(curWin)) {
      windowHandles.push(curWin);
      driver.switchTo().window(handle);
      break;
    }
  }
}

代码示例来源:origin: appium/java-client

@Test public void listenableObjectSample() {
    try {
      ContextAware listenableContextAware =
          getEventFiringObject(contextAware, emptyWebDriver, contextListener, alertListener);
      WebDriver webDriver = listenableContextAware.context("NATIVE_APP");
      assertTrue(contextAwarePredicate.test(listenableContextAware));

      Alert alert = webDriver.switchTo().alert();
      assertTrue(alertPredicate.test(alert));

      assertTrue(webDriverPredicate.test(getEventFiringWebDriver(webDriver, searchingListener)));
    } finally {
      listeners.get(ContextListener.class).messages.clear();
      listeners.get(AlertListener.class).messages.clear();
      listeners.get(SearchingListener.class).messages.clear();
    }
  }
}

相关文章