简体   繁体   English

使用JavascriptExecutor发送Keys并单击web元素

[英]Using JavascriptExecutor to sendKeys plus click on web element

I'm trying to open a link in a new tab, then switch to that tab, in a Firefox browser, using selenium in Java. 我正在尝试在新选项卡中打开一个链接,然后在Firefox浏览器中使用Java中的selenium切换到该选项卡。 It's my understanding that in order to do this, I need to use a send keys combination. 我的理解是,为了做到这一点,我需要使用发送键组合。

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. 我无法弄清楚sendKeys到JavascriptExecutor的正确语法。 I've seen some similar solutions using Actions, but this hasn't worked for me either. 我已经看过一些使用Actions的类似解决方案,但这对我来说也没有用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在selenium中使用JavascriptExecutor右键单击svg元素 - How to right click on a svg element using JavascriptExecutor in selenium javascriptexecutor-无法单击带有身体样式标签为“ overflow:hidden”的元素 - javascriptexecutor -Not able to click element with body style tag as “overflow:hidden” JavaScriptExecutor中的参考硒元素 - Reference selenium element in JavaScriptExecutor Selenium WebDriver(Java):JavascriptExecutor无法单击元素,而WebElement.click()可以正常工作 - Selenium WebDriver(Java) : JavascriptExecutor fails to click element, while WebElement.click() works well 无法在输入元素上使用sendKeys上传文件 - Unable to upload file using sendKeys on input element 在Powershell中使用JavascriptExecutor - Using the JavascriptExecutor with Powershell 使用 JavascriptExecutor 的 Selenium Datepicker - Selenium Datepicker using JavascriptExecutor 尝试使用selenium webdriver使用JavascriptExecutor单击单选按钮时获取错误 - Get Error while try to click on radio button using selenium webdriver using JavascriptExecutor 如何在 Facebook 等无限屏幕上使用 JavaScriptExecutor 在一段时间后停止寻找元素? - How to stop looking for an element after a certain period using JavaScriptExecutor on an unlimited screen like Facebook? RGraph 通过单击关键元素隐藏线加标签 - RGraph hide line plus labels with a click on key element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM