新标签未在Selenium中打开
import org.testng.annotations.Test;
public class SimpleTest {
@Test
public void TestMethod() throws InterruptedException
{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
WebElement element=driver.findElement(By.linkText("Gmail"));
Thread.sleep(3000);
element.sendKeys(Keys.CONTROL,"t");
}
}
请帮我找到错误
答案 0 :(得分:0)
在新标签页中打开链接。
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("http://www.google.com/")).sendKeys(selectLinkOpeninNewTab);
答案 1 :(得分:0)
以下是工作代码..将Gmail打开到新标签页并切换到它。
import org.testng.annotations.Test;
public class SimpleTest {
@Test
public void TestMethod() throws InterruptedException{
System.setProperty("webdriver.gecko.driver","C:\\22November2017\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.get("http://www.google.com/");
Thread.sleep(3000);
String selectLinkOpeninNewTab = Keys.chord(Keys.CONTROL,Keys.RETURN);
driver.findElement(By.linkText("Gmail")).sendKeys(selectLinkOpeninNewTab);
Thread.sleep(3000);
ArrayList<String> tab = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tab.get(1));
}
}
答案 2 :(得分:0)
要使用linkText("Gmail")
打开新标签,您可以使用以下代码块:
String URL="http://www.google.com";
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get(URL);
System.out.println("Page Title is : "+driver.getTitle());
WebElement link = driver.findElement(By.linkText("Gmail"));
Actions newTab = new Actions(driver);
newTab.keyDown(Keys.CONTROL).click(link).keyUp(Keys.CONTROL).build().perform();
答案 3 :(得分:0)
您可以使用JavaScriptExecutor来执行此操作
Javascriptexecutor js = (Javascriptexecutor)driver;
js.executescript("var win = window.open('"+ YourURLHere + "', '_blank');win.focus(); ");
上面的代码将打开新标签并导航到提供的网址,并专注于新标签。