问题
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/37492102/action-sendkeysbody-keys-control-j-dosent-open-the-download-page