最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Using JavascriptExecutor to sendKeys plus click on web element - Stack Overflow

programmeradmin1浏览0评论

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 bination.

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.

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 bination.

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.

Share Improve this question edited Jul 29, 2015 at 4:41 GNUser asked Jul 26, 2015 at 2:01 GNUserGNUser 1,7566 gold badges33 silver badges52 bronze badges 3
  • When you've used Actions to perform CTRL+SHIFT+click, what happened? Thanks. – alecxe Commented Jul 26, 2015 at 2:03
  • @alecxe Actually, if I use new Actions(driver).keyDown(Keys.CONTROL).keyDown(Keys.SHIFT).click(we).keyUp(Keys.C‌​ONTROL).keyUp(Keys.SHIFT).perform();, then the link is opened in the current tab. I can't figure out how to make this work properly either. – GNUser Commented Jul 26, 2015 at 3:36
  • Why do you want to execute sendKeys using Actions or JavascriptExecutor? Plain Selenium WebElement doesn't work? – user4925383 Commented Jul 26, 2015 at 10:07
Add a ment  | 

1 Answer 1

Reset to default 1

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./");
        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
发布评论

评论列表(0)

  1. 暂无评论