Java 如何在元素上单击鼠标右键并在selenium浏览器中选择一个选项
我正在自动化一个页面,我想右键单击一个元素,然后单击“另存链接为…”,我可以右键单击该元素,但无法选择任何选项。下面是我编写的代码,但它没有选择选项,而是实际单击该元素Java 如何在元素上单击鼠标右键并在selenium浏览器中选择一个选项,java,selenium,google-chrome,webdriver,selenium-chromedriver,Java,Selenium,Google Chrome,Webdriver,Selenium Chromedriver,我正在自动化一个页面,我想右键单击一个元素,然后单击“另存链接为…”,我可以右键单击该元素,但无法选择任何选项。下面是我编写的代码,但它没有选择选项,而是实际单击该元素 WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]")); Actions builderq = new Actions(driver); builderq
WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]"));
Actions builderq = new Actions(driver);
builderq.contextClick(elementq).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER);
builderq.build().perform();
以下是HTML:
(100KB)
Asim Sarfraz,2018年9月25日上午8:24
尝试使用对象解决此问题:
Actions builderq=新操作(驱动程序);
builderq.contextClick(elementq);
builderq.build().perform();
机器人=新机器人();
int重复输入值为3的输入值;
for(int i=0;i<重复VK下的输入;i++){
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyerelease(KeyEvent.VK_DOWN);
睡眠(500);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
睡眠(1000);
评论太大,所以作为答案发布
根据您的问题,您希望上下文单击(元素)
,然后通过操作选择将链接另存为
值得一提的是,如果上下文菜单列表被实现为本机上下文菜单列表,我们可以很容易地解决这个问题。但根据您共享的HTML,上下文菜单列表似乎是Chrome浏览器的本机上下文菜单列表
根据:
转到渲染器进程contextClick(元素)
- 这是ChromeDriver模拟按键的方式的一个限制
- 可以称为ChromeDriver架构的缺陷或限制,我们必须等待问题得到解决
WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]"));
String imageLink = logo.getAttribute("href");
String fileName = url.getFile();
URL imageURL = new URL(imageLink);
InputStream in = new BufferedInputStream(imageURL.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
for ( int i; (i = in.read()) != -1; ) {
out.write(i);
}
in.close();
out.close();
显示选项的可视视图(pic),您要选择的选项将有助于更好地理解问题。您可以使用Robot对象,也可以使用(ctrl+k)来“保存链接”。这就是示例。
WebElement elementq =driver.findElement(By.xpath("//a[contains(text(),'fedev.docs-gmail.JPG')][@class]"));
String imageLink = logo.getAttribute("href");
String fileName = url.getFile();
URL imageURL = new URL(imageLink);
InputStream in = new BufferedInputStream(imageURL.openStream());
OutputStream out = new BufferedOutputStream(new FileOutputStream(fileName));
for ( int i; (i = in.read()) != -1; ) {
out.write(i);
}
in.close();
out.close();