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(); } }
代码说明:仅做测试,上传功能大家套用至自己需要测试的页面即可