WebDriver高级应用实例(3)

  3.1自动化下载某个文件

  被测网页的网址:

  https://pypi.org/project/selenium/#files

  Java语言版本的API实例代码

import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
 
public class download {
    WebDriver driver;
    String url="https://pypi.org/project/selenium/#files";
    @Test
    public void testOne() throws Exception {            
        //使用Chrome浏览器自动下载文件并保存到指定的文件路径
        //或 使用Selenium更改Chrome默认下载存储路径
        DesiredCapabilities caps = setDownloadsPath();//更改默认下载路径        
        driver = new ChromeDriver(caps);
        driver.manage().window().maximize();
        driver.get(url);
        WebElement myElement = driver.findElement(By.xpath("//a[contains(text(),'selenium-3.141.0.tar.gz')]"));
        Actions action = new Actions(driver);
        myElement.click();//点击下载
        Thread.sleep(10000);
    }
    
    //单独重构成一个方法,然后调用
    public DesiredCapabilities setDownloadsPath() {
        String downloadsPath = "E:\\downloadFiles";
        HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
        chromePrefs.put("download.default_directory", downloadsPath);
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("prefs", chromePrefs);
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability(ChromeOptions.CAPABILITY, options);
        return caps;
    }
    
      @BeforeMethod
      public void beforeMethod() {
          System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
          driver = new ChromeDriver();
          driver.manage().window().maximize();
        
      }

      @AfterMethod
      public void afterMethod() {
          driver.quit();
      }
}

  3.2使用sendKeys方法上传一个文件附件

  被测网页的网址: 

  <html>
    <body>
      <from enctype="multipart/form-data" action="parse_file" method="post">
        <p>Browse for a file to upload:</P>
        <input id="file" name="file" type="file"></input>
        <br/><br/>
        <input type="submit" id="filesubmit" value="SUMBMIT"></input>
      </from>
    </body>
  </html>

  Java语言版本的API实例代码

import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;

public class upLoad {
    WebDriver driver;
    String url ="file:///E:/%E6%9D%90%E6%96%99/selenium/upload.html";
  @Test
  public void testUploadFile() {
      driver.get(url);
      //找到文件上传输入框
      WebElement fileInputBox = driver.findElement(By.id("file"));
      //输入文件路径
      fileInputBox.sendKeys("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg");
      //设置显示等待5秒
      WebDriverWait wait = new WebDriverWait(driver, 5);
     //显示等待判断页面按钮是否处于可单击状态
      wait.until(ExpectedConditions.elementToBeClickable(By.id("filesubmit")));
      //找到提交按钮
      WebElement fileSubMitButton = driver.findElement(By.id("filesubmit"));
      //单击提交按钮
      fileSubMitButton.click();
      
  }
  @BeforeMethod
  public void beforeMethod() {
      System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");
      driver = new ChromeDriver();
      driver.manage().window().maximize();
  }

  @AfterMethod
  public void afterMethod() {
      driver.quit();
  }

}

  代码说明:仅做测试,上传功能大家套用至自己需要测试的页面即可

转载于:https://www.cnblogs.com/z-zzz/p/10509066.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值