action.sendKeys(body, Keys.CONTROL + “j”) dosen

2019-04-16 19:51发布

I am using Java and Selenium to write tests for Chrome. I need to open the download page at one point so I used:

action.sendKeys(Keys.CONTROL + "j").build().perform();

but it does NOT open the page. then I added this line before it as I though it might work but it didn't:

WebElement body = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//body[@id='body']")));
action.sendKeys(body, Keys.CONTROL + "j").build().perform();

The weird point is that

action.sendKeys(Keys.CONTROL + "a").build().perform();

Works!!

NOTE: I do not want to use Robot class as it will open the page on other browsers if the focus is not on the test target browser.

2条回答
女痞
2楼-- · 2019-04-16 20:10

Try using like Below code, it is working perfectly at my end ...

Actions builder = new Actions(driver);
builder.keyDown(Keys.CONTROL).sendKeys("j").keyUp(Keys.CONTROL).build().perform();

I'd suggest not to go for chrome://downloads as this will not work in IE and FF but CTRL + j will work on all chrome, IE and FF.

查看更多
3楼-- · 2019-04-16 20:13

Do you need to use the key binding? You could just navigate to the URL chrome://downloads/ ...

Edit: Ofc, this is not the ideal solution for cross-browser compability. But definitely a good workaround for chrome.

查看更多
登录 后发表回答