javascript 使用 JavascriptExecutor 发送键并单击 web 元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31632923/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:06:14  来源:igfitidea点击:

Using JavascriptExecutor to sendKeys plus click on web element

javascriptjavafirefoxseleniumsendkeys

提问by ch-pub

I'm trying to open a link in a new tab, then switch to that tab, in a Firefox browser, using selenium in Java. It's my understanding that in order to do this, I need to use a send keys combination.

我正在尝试在新选项卡中打开一个链接,然后在 Firefox 浏览器中使用 Java 中的 selenium 切换到该选项卡。我的理解是,为了做到这一点,我需要使用发送组合键。

In order to open the link in the same window, I've been using something like this:

为了在同一个窗口中打开链接,我一直在使用这样的东西:

WebElement we = driver.findElement(By.xpath("//*[@id='btn']"));

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", we);

The above was working fine for me.

以上对我来说工作正常。

Now I'm trying to also sendKeys, as in below, which is not working:

现在我还尝试发送密钥,如下所示,但它不起作用:

JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("keyDown(Keys.CONTROL)
                        .keyDown(Keys.SHIFT)
                        .click(arguments[0])
                        .keyUp(Keys.CONTROL)
                        .keyUp(Keys.SHIFT);", we);

Any advice? I can't figure out the correct syntax to sendKeys to JavascriptExecutor. I've seen some similar solutions using Actions, but this hasn't worked for me either.

有什么建议吗?我无法弄清楚将 sendKeys 发送到 JavascriptExecutor 的正确语法。我已经看到一些使用 Actions 的类似解决方案,但这对我也不起作用。

采纳答案by Deepak

try below code to open any link on page to new tab & switch to that tab. Perform operations there & go back to first tab for further execution.

尝试下面的代码打开页面上的任何链接到新标签并切换到该标签。在那里执行操作并返回第一个选项卡以进一步执行。

WebDriver driver = new FirefoxDriver();
        driver.get("http://stackoverflow.com/");
        WebElement e = driver.findElement(By.xpath(".//*[@id='nav-questions']"));       
        Actions action = new Actions(driver); 
        action.keyDown(Keys.CONTROL).build().perform(); //press control key
        e.click();
        Thread.sleep(10000); // wait till your page loads in new tab
        action.keyUp(Keys.CONTROL).build().perform(); //release control key
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //move to new tab
        driver.navigate().refresh(); // refresh page
        driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo
        driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //switch to first tab
        driver.navigate().refresh(); 
        driver.findElement(By.xpath(".//*[@id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo