Java 如何单击“在新选项卡中打开链接”以使用selenium webdriver在主选项卡中打开“在新选项卡中打开链接”

Java 如何单击“在新选项卡中打开链接”以使用selenium webdriver在主选项卡中打开“在新选项卡中打开链接”,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我正在尝试使用以下代码自动执行google搜索: driver.get("google.com"); driver.findelement(By.id("lst-ib").sendkeys("search"); driver.findelement(By.classname(lsb)).click; 在新选项卡的搜索框中输入search关键字后,我试图打开建议链接,即在同一窗口的5个新选项卡中输入5个链接 我写的代码: ActionChains(driver).key_down(Keys.CO

我正在尝试使用以下代码自动执行google搜索:

driver.get("google.com");
driver.findelement(By.id("lst-ib").sendkeys("search");
driver.findelement(By.classname(lsb)).click;
在新选项卡的搜索框中输入
search
关键字后,我试图打开建议链接,即在同一窗口的5个新选项卡中输入5个链接 我写的代码:

ActionChains(driver).key_down(Keys.COMMAND).send_keys("t").key_up(Keys.COMMAND)‌​‌​.perform();

但这不是在新选项卡中打开链接

您可以在按住Ctrl键的同时单击以下按钮来实现在新选项卡中打开链接:

new Actions(driver)
    .KeyDown(Keys.Control)
    .Click(element)
    .KeyUp(Keys.Control)
    .Perform(); // C# syntax, as I'm not familiar with Java...

通过按住Ctrl键并单击,可以实现在新选项卡中打开:

new Actions(driver)
    .KeyDown(Keys.Control)
    .Click(element)
    .KeyUp(Keys.Control)
    .Perform(); // C# syntax, as I'm not familiar with Java...

这就是我在使用下面提到的代码后获得焦点并右键单击并在新选项卡上打开的原因:

link=driver.find_element_by_xpath("//*[contains(text(),'After Life (TV Series 2019– ) - IMDb')]")
actionChains = ActionChains(driver)
actionChains.context_click(link).perform()
ac=driver.switch_to.active_element
actionChains.key_down(Keys.CONTROL).click(ac).key_up(Keys.CONTROL).perform()

这就是我在使用下面提到的代码后获得焦点并右键单击并在新选项卡上打开的原因:

link=driver.find_element_by_xpath("//*[contains(text(),'After Life (TV Series 2019– ) - IMDb')]")
actionChains = ActionChains(driver)
actionChains.context_click(link).perform()
ac=driver.switch_to.active_element
actionChains.key_down(Keys.CONTROL).click(ac).key_up(Keys.CONTROL).perform()

您好,您能建议我如何聚焦到该链接,然后返回到上一个选项卡吗?您好,您能建议我如何聚焦到该链接,然后返回到上一个选项卡吗