我正在自动化一个页面,我想在该页面上右键单击一个元素,然后单击“链接另存为...。我可以右键单击该元素,但无法选择任何选项。下面是我的代码已经写过,但实际上并未点击选项,而是点击了该元素。
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:
<p><a class="icon icon-attachment" href="/attachments/download/15535/fedev.docs-gmail.JPG">fedev.docs-gmail.JPG</a>
<span class="size">(100 KB)</span>
<a data-confirm="Are you sure?" class="delete" title="Delete" rel="nofollow" data-method="delete" href="/attachments/15535"><img src="/images/delete.png" alt="Delete"></a>
<span class="author">Asim Sarfraz, 25 September 2018 08:24 AM</span>
</p>
答案 0 :(得分:1)
如果您的目的是从网页上下载图像,则无需右键单击即可直接使用url下载图像。
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();
答案 1 :(得分:0)
尝试使用Robot对象解决此问题:
Actions builderq = new Actions(driver);
builderq.contextClick(elementq);
builderq.build().perform();
Robot robot = new Robot();
int REPEATS_OF_VK_DOWN_INPUT = 3;
for (int i = 0; i < REPEATS_OF_VK_DOWN_INPUT; i++) {
robot.keyPress(KeyEvent.VK_DOWN);
robot.keyRelease(KeyEvent.VK_DOWN);
Thread.sleep(500);
}
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(1000);
答案 2 :(得分:-1)
评论太大,因此发布为答案。
根据您的问题,您想 contextClick(element)
,然后通过“操作”选择 Save link as
。
值得一提的是,如果 context-menu-list 已实现为 native context-menu-list ,我们可以很容易地解决。但是,按照您共享的HTML,看来 context-menu-list 是 Chrome浏览器的 本机context-menu-list 。
>根据Chromedriver does not send keys to context menu:
contextClick(element)
进入渲染器过程。