WebDriver操作tab
核心思想:运用FireFox或者其他浏览器的键盘快捷键,然后使用Action进行操作。
FireFox快捷键:https://support.mozilla.org/en-US/kb/keyboard-shortcuts-perform-firefox-tasks-quickly
下面是代码示例:
package webdriver; import java.util.logging.Logger; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.interactions.Actions; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; public class TestWebDriver { private static final Logger logger = Logger.getLogger(TestWebDriver.class.getName()); private WebDriver webDriver; @BeforeClass public void beforeClass(){ webDriver = new FirefoxDriver(); } @Test public void f(){ webDriver.get("http://www.sohu.com"); String title = webDriver.getTitle(); Actions actions = new Actions(webDriver); //open a new tab actions.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).perform(); //change to the second window while(true){ actions.keyDown(Keys.CONTROL).sendKeys(Keys.TAB).keyUp(Keys.CONTROL).perform(); logger.info(webDriver.getTitle()); if(!title.equals(webDriver.getTitle())){ break; } } } @AfterClass public void afterClass(){ webDriver.quit(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。