在页面上有一个类似这样的元素:
<a href="javascript:void(0)"> Computer durchsuchen </a>
如果单击此链接,将打开一个窗口,您可以在其中选择图像(路径)。我设法点击了链接,窗口就打开了。我的问题是,现在我如何通过路径,如何再次关闭它?
This is the site it concerns...
这是我到目前为止编写的代码:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.shirtee.com/de/designer/?id=1140/");
WebElement link = driver.findElement(By.cssSelector(".upload-image-drop-zone a"));
Actions ob = new Actions(driver);
ob.click(link);
org.openqa.selenium.interactions.Action action = ob.build();
action.perform();
Here you can see a screenshot of the processes that happen...
发布于 2018-06-28 05:00:50
由于Selenium
不能与本机窗口交互,因此可以对其使用Robot
类:
Robot robot = new Robot();
StringSelection selection = new StringSelection("Absolute path of the file");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
完整的代码如下所示:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
public class Test {
public static void main(String[] args) throws InterruptedException, AWTException {
final WebDriver driver = new ChromeDriver();
driver.get("https://www.shirtee.com/de/designer/?id=1140/");
WebElement link = driver.findElement(By.cssSelector(".upload-image-drop-zone a"));
link.click();
Thread.sleep(500); // small pause
Robot robot = new Robot();
StringSelection selection = new StringSelection("C:\\full\\path\\to\\file\\image.png");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Thread.sleep(3000); // only to see the result
driver.quit();
}
}
有关详细信息,请参阅documentation here。
https://stackoverflow.com/questions/51071082
复制相似问题