WebDriver高级应用实例(3)
生活随笔
收集整理的這篇文章主要介紹了
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";@Testpublic 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;}@BeforeMethodpublic void beforeMethod() {System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");driver = new ChromeDriver();driver.manage().window().maximize();}@AfterMethodpublic 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";@Testpublic 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();}@BeforeMethodpublic void beforeMethod() {System.setProperty("webdriver.chrome.driver", "D:\\WebDriver\\chromedriver_win32\\chromedriver.exe");driver = new ChromeDriver();driver.manage().window().maximize();}@AfterMethodpublic void afterMethod() {driver.quit();}}代碼說明:僅做測試,上傳功能大家套用至自己需要測試的頁面即可
轉載于:https://www.cnblogs.com/z-zzz/p/10509066.html
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的WebDriver高级应用实例(3)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CF248E Piglet's Birt
- 下一篇: freemaker中小数展示为整数的问题