我正在尝试使用selenium Java中的Actions和Robot类保存图像文件。我通过contextclick()右键单击图像,然后使用CTRL + v作为另存为图像选项。但是,我无法访问“另存为”的窗口对话框。我也无法调试,因为调试也没有把我带到那条线,所以我可以检查问题。
以下是代码:
WebElement imgRightClick=driver.findElement(By.xpath(".//table[contains(@id,'WebPart_twp285729713')]//img"));
Actions action= new Actions(driver);
action.contextClick(imgRightClick).sendKeys(Keys.CONTROL, "v").build().perform();
Thread.sleep(2000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_H);
robot.keyRelease(KeyEvent.VK_H);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_COLON);
robot.keyRelease(KeyEvent.VK_COLON);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_BACK_SLASH);
robot.keyRelease(KeyEvent.VK_BACK_SLASH);
Thread.sleep(2000);
robot.keyPress(KeyEvent.VK_ENTER);
Thread.sleep(3000L);
请查看上述问题。
答案 0 :(得分:0)
完成这项工作的诀窍很简单。您手动右键单击图像并点击“另存为”。现在尝试使用键盘在向导上输入文件路径和“保存”按钮。
您可以使用“TAB”将控制切换到各种元素,然后按“ENTER”进行单击。
使用Robot Class实现相同的键盘流程!
希望它有所帮助!
答案 1 :(得分:0)
这是下载图片的示例代码,对我来说很好。
import java.awt.Robot;
import java.awt.event.KeyEvent;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
public class Sample {
public static void main(String[] args) throws Exception
{
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in");
driver.findElement(By.name("q")).sendKeys("selenium");
Actions action= new Actions(driver);
action.contextClick(driver.findElement(By.id("logo")).findElement(By.tagName("img"))).build().perform();
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).build().perform();
Thread.sleep(2000);
action.sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).build().perform();
Thread.sleep(2000);
Robot RoboKey = new Robot();
RoboKey.keyPress(KeyEvent.VK_W);
RoboKey.keyPress(KeyEvent.VK_A);
RoboKey.keyPress(KeyEvent.VK_M);
RoboKey.keyPress(KeyEvent.VK_A);
RoboKey.keyPress(KeyEvent.VK_N);
RoboKey.keyPress(KeyEvent.VK_PERIOD);
RoboKey.keyPress(KeyEvent.VK_J);
RoboKey.keyPress(KeyEvent.VK_P);
RoboKey.keyPress(KeyEvent.VK_G);
Thread.sleep(2000);
RoboKey.keyPress(KeyEvent.VK_ENTER);
}
}
希望它有所帮助!