Java 如何使用Actions类在chrome中打开新选项卡
如何在chrome上打开新选项卡。我需要从中获取一些数据,返回到上一个选项卡并输入数据。我知道如何遍历选项卡,但无法打开新选项卡 硒版本:3.5.2Java 如何使用Actions类在chrome中打开新选项卡,java,eclipse,selenium,selenium-webdriver,Java,Eclipse,Selenium,Selenium Webdriver,如何在chrome上打开新选项卡。我需要从中获取一些数据,返回到上一个选项卡并输入数据。我知道如何遍历选项卡,但无法打开新选项卡 硒版本:3.5.2 Chrome版本:60 package Amazon; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.Keys; import org.openqa.selenium.WebDriver; impor
Chrome版本:60
package Amazon;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class VerifyAmazonSignInPage {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C://Selenium jars/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.amazon.in");
driver.findElement(By.xpath("//span[text()='Hello. Sign in']")).click();
driver.findElement(By.id("ap_email")).sendKeys("seleniumoar1234@gmail.com");
driver.findElement(By.id("ap_password")).sendKeys("*****");
driver.findElement(By.id("signInSubmit")).click();
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("t").keyUp(Keys.CONTROL).build().perform();
driver.get("http://www.gmail.com");
}
}
我使用的经验法则是,仅用于模拟最终用户的鼠标操作。不要把它用于其他目的
在您的情况下,您需要向浏览器发送密钥,您可以使用WebDriver接口的任何实现来实现。那么,您希望如何打开一个新选项卡
最简单的方法是发送body元素上的键以打开新选项卡-
driver.findElement(By.tagName("body")).sendKeys(Keys.CONTROL + "t");
但在一些场景中,这不起作用,所以我更喜欢,我已经写过了,因为它是平台无关的解决方案
((JavascriptExecutor) driver).executeScript("window.open('http://gmail.com/','_blank');");
请注意,打开新选项卡后,您必须注意它不会自动工作 Action类是一个selenium类,它只能用于自动化基于Web的应用程序。打开新选项卡是在webbrowser上执行的操作,webbrowser是一个独立的应用程序。这可以通过使用java.awt包中的Robot类来完成。对于不同版本的selenium和不同的浏览器,这可能是重复的。为什么必须使用
Action
class?@4M01我知道我可以使用Robot类,但我想知道是否可以使用Actions类来完成。谢谢@4M01我想知道是否可以使用Actions类来完成,但无论如何,这也很有帮助。@GauravThantryAction
类也应该可以,但大多数时候都失败了。这就是我到目前为止观察到的情况SendKeys
打开新选项卡不是一种好的做法,它要求浏览器处于活动状态,焦点在浏览器上。但是,不管怎样,Javascript都可以工作