zoukankan      html  css  js  c++  java
  • Selenium上传图片

    import org.openqa.selenium.By;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.testng.Assert;
    import org.testng.annotations.Test;

    import java.awt.*;
    import java.awt.datatransfer.StringSelection;
    import java.awt.event.KeyEvent;

    public class BaiduUploadImg {
    @Test
    public void baiduUploadImg()
    throws InterruptedException, AWTException {
    System.setProperty("webdriver.firefox.marionette",
    "src/main/resourcec/geckodriver.exe");
    String baiduHomePage;
    baiduHomePage = "https://www.baidu.com/";
    WebDriver driver;
    driver = new FirefoxDriver();
    driver.manage().window().maximize();
    driver.get(baiduHomePage);
    Thread.sleep(2000);
    Assert.assertEquals(driver.getTitle(),
    "百度一下,你就知道");

    driver.findElement
    (By.xpath(".//*[@id='form']/span[1]/span")).click();
    //点击相机图标
    Thread.sleep(2000);
    WebElement element = driver.findElement
    (By.xpath(".//*[@id='form']/div/div[2]/div[2]/input"));
    ((JavascriptExecutor) driver).executeScript
    ("arguments[0].click();", element);
    //模拟JS点击本地上传图片
    Thread.sleep(2000);

    String path = System.getProperty("user.dir")+
    "\src\main\resources\img.jpg";
    StringSelection stringSelection = new StringSelection(path);
    //指定图片的路径,使用相对路径
    Toolkit.getDefaultToolkit().getSystemClipboard()
    .setContents(stringSelection, null);
    //把图片路径复制粘贴到剪切板

    Robot robot = new Robot();
    robot.keyPress(KeyEvent.VK_ENTER);
    //按下Enter键
    robot.keyRelease(KeyEvent.VK_ENTER);
    //释放Enter键

    robot.keyPress(KeyEvent.VK_CONTROL);
    robot.keyPress(KeyEvent.VK_V);
    //按下Ctrl+V
    robot.keyRelease(KeyEvent.VK_CONTROL);
    robot.keyRelease(KeyEvent.VK_V);
    //释放Ctrl+V

    robot.keyPress(KeyEvent.VK_ENTER);
    //再次点击Enter键
    robot.keyRelease(KeyEvent.VK_ENTER);
    //再次释放Enter键

    Thread.sleep(3000);
    Assert.assertEquals(driver.getTitle(),
    "百度识图——以图搜信息,发现更多可能");
    driver.close();
    driver.quit();
    }
    }
  • 相关阅读:
    Firefox功能强大的浏览器 (转)
    常用的dnet开源项目
    15 个 JavaScript Web UI 库
    关于Web路径的备忘
    推荐几个.NET开源图表组件(转)
    C#开源资源大汇总 (转)
    jQuery对select操作小结 转载
    非对称加密RSA的应用及在C#中的实现(转)
    Web开发人员应当知道的15个开源项目
    css中float和列表图片liststyleimage不能正常解析的说明
  • 原文地址:https://www.cnblogs.com/yjlch1016/p/8321754.html
Copyright © 2011-2022 走看看