webdriver---API---(java版) 高级应用2
Posted 跳动de手指
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了webdriver---API---(java版) 高级应用2相关的知识,希望对你有一定的参考价值。
1、如何无人工接入的下载某个文件
package china; import org.testng.annotations.Test; import com.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxProfile; import org.testng.annotations.AfterMethod; public class testDatapicker { public WebDriver driver; String baseUrl; JavaScriptExecutor js; public static String downloadFilePath="D:\\downloadFiles"; @Test public void f() throws Exception { driver=new FirefoxDriver(FirefoxDriverProfile()); driver.get(baseUrl); driver.findElement(By.partialLinkText("电信")).click(); try{ Thread.sleep(10000); }catch(Exception e){ e.printStackTrace(); } } public static FirefoxProfile FirefoxDriverProfile()throws Exception{ FirefoxProfile profile=new FirefoxProfile(); profile.setPreference("browser.download.folderList", 2); profile.setPreference("browser.download.manager.showWhenStarting", false); profile.setPreference("browser.download.dir", downloadFilePath); profile.setPreference("browser.helperApps.neverAsk.openFile", "application/octet-stream,application.exe,text/csv,application/pdf,application/x-msexcel,application/excel,application/x-excel,application/vnd.ns-excel,application/xml,text/html,image/png"); profile.setPreference("browser.helperApps.neverAsk.savaToDisk", "application/octet-stream,application.exe,text/csv,application/pdf,application/x-msexcel,application/excel,application/x-excel,application/vnd.ns-excel,application/xml,text/html,image/png" ); profile.setPreference("browser.helperApps.alwayAsk.force", false); profile.setPreference("browser.download.manager.alertOnEXEOpen", false); profile.setPreference("browser.download.manager.forcusWhenStarting",false); profile.setPreference("browser.download.manager.useWindow", false); profile.setPreference("browser.download.manager.showAlertOnComlplet", false); profile.setPreference("browser.download.manager.closeWhenDone", false); return profile; } @BeforeMethod public void beforeMethod() { baseUrl="http://rj.lequ66.top/soft/3033.html"; System.setProperty("webdriver.gecko.driver", "C:\\firefoxdriver\\geckodriver.exe"); } @AfterMethod public void afterMethod() { driver.quit(); } }
2、使用sendkeys方法上传一个文件附件
<html> <head> <title>SENDKEYS传送文件附件</title> </head> <body> <form enctype="multipart/form-data" action="parse_file.jsp""method="post"> <p>Browser for a file to upload:</p> <input id="file" name="file" type="file"></input> <br/><br/> <input type="submit" id="filesubmit" value="SUBMIT"></input> </form> </body> </html>
package china; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import java.io.File; 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 sendKeysAPI { WebDriver driver; String baseUrl; @Test public void testLoadFile()throws Exception { WebElement fileInputBox=driver.findElement(By.id("file")); fileInputBox.sendKeys("D:\\a.txt"); WebDriverWait wait=new WebDriverWait(driver, 5); wait.until(ExpectedConditions.elementToBeClickable(By.id("filesubmit"))); WebElement submitbutton=driver.findElement(By.id("filesubmit")); submitbutton.click(); wait.until(ExpectedConditions.titleContains("文件上传成功")); } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.chrome.driver","C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); File file=new File("D:\\workspace\\webdriver Api\\src\\sendkeys.html"); String filepath=file.getAbsolutePath(); driver.get(filepath); } @AfterMethod public void afterMethod() { } }
上传文件可以使用第三方工具Autolt来完成一些webdriver无法操作的文件上传对象,具体使用方法百度;
3、操作web页面的滚动条
package china; import org.testng.annotations.Test; import net.sourceforge.htmlunit.corejs.javascript.ast.CatchClause; import org.testng.annotations.BeforeMethod; import org.openqa.selenium.By; import org.openqa.selenium.JavascriptExecutor; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.annotations.AfterMethod; public class scrollbarApi { WebDriver driver; String baseUrl; //将滚动条滑动到页面最下方 @Test(priority=0) public void f() { ((JavascriptExecutor)driver).executeScript("window.scrollTo(0,document.body.scrollHeight)"); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } } //滑动到指定元素位置 @Test(priority=1) public void scroll(){ //driver.switchTo().frame("main_frame"); WebElement element=driver.findElement(By.xpath(".//*[@id=‘container‘]/div[2]/div[4]/div[2]/div[1]/h3/a")); ((JavascriptExecutor)driver).executeScript("arguments[0].scrollIntoView();",element); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } } //将滑动条向下滑动800像素 @Test(priority=2) public void scrollto(){ ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,800)"); try{ Thread.sleep(3000); }catch(InterruptedException e){ e.printStackTrace(); } } @BeforeMethod public void beforeMethod() { System.setProperty("webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); driver=new ChromeDriver(); baseUrl="http://v.sogou.com"; driver.navigate().to(baseUrl); } @AfterMethod public void afterMethod() { try{Thread.sleep(3000);}catch(InterruptedException e){ e.printStackTrace(); } driver.quit(); } }