1, how to download a file without manual access
PackageChina ;Importorg.testng.annotations.Test;ImportCom.gargoylesoftware.htmlunit.javascript.background.JavaScriptExecutor;ImportOrg.testng.annotations.BeforeMethod;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;ImportOrg.openqa.selenium.firefox.FirefoxDriver;ImportOrg.openqa.selenium.firefox.FirefoxProfile;ImportOrg.testng.annotations.AfterMethod; Public classTestdatapicker { PublicWebdriver Driver; String BaseUrl; Javascriptexecutor JS; Public StaticString downloadfilepath= "D:\\downloadfiles"; @Test Public voidF ()throwsException {driver=NewFirefoxdriver (Firefoxdriverprofile ()); Driver.get (BASEURL); Driver.findelement (By.partiallinktext (Telecommunications) . Click (); Try{Thread.Sleep (10000); }Catch(Exception e) {e.printstacktrace (); } } Public StaticFirefoxprofile Firefoxdriverprofile ()throwsexception{firefoxprofile profile=NewFirefoxprofile (); 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); returnProfile ; } @BeforeMethod Public voidBeforemethod () {BASEURL= "Http://rj.lequ66.top/soft/3033.html"; System.setproperty ("Webdriver.gecko.driver", "C:\\firefoxdriver\\geckodriver.exe"); } @AfterMethod Public voidAftermethod () {driver.quit (); }}
2. Upload a file attachment using the SendKeys method
<HTML><Head><title>SendKeys Transferring file attachments</title></Head><Body> <formenctype= "Multipart/form-data"Action= "parse_file.jsp""Method= "POST"> <P>Browser for a file to upload:</P> <inputID= "File"name= "File"type= "File"></input> <BR/><BR/> <inputtype= "Submit"ID= "Filesubmit"value= "SUBMIT"></input> </form> </Body></HTML>
PackageChina ;Importorg.testng.annotations.Test;ImportOrg.testng.annotations.BeforeMethod;ImportJava.io.File;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver;Importorg.openqa.selenium.support.ui.ExpectedConditions;Importorg.openqa.selenium.support.ui.WebDriverWait;ImportOrg.testng.annotations.AfterMethod; Public classSendkeysapi {webdriver driver; String BaseUrl; @Test Public voidTestloadfile ()throwsException {webelement Fileinputbox=driver.findelement (by.id ("File")); Fileinputbox.sendkeys ("D:\\a.txt"); webdriverwait wait=NewWebdriverwait (Driver, 5); Wait.until (Expectedconditions.elementtobeclickable (By.id ("Filesubmit"))); Webelement Submitbutton=driver.findelement (By.id ("Filesubmit")); Submitbutton.click (); Wait.until (Expectedconditions.titlecontains ("File Upload succeeded")); } @BeforeMethod Public voidBeforemethod () {System.setproperty ("Webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); File File=NewFile ("D:\\workspace\\webdriver api\\src\\sendkeys.html"); String filepath=File.getabsolutepath (); Driver.get (filepath); } @AfterMethod Public voidAftermethod () {}}
Upload files can use third-party tools Autolt to complete some webdriver unable to manipulate the file upload object, the specific use of Baidu;
3, the operation of the Web page scroll bar
PackageChina ;Importorg.testng.annotations.Test;ImportNet.sourceforge.htmlunit.corejs.javascript.ast.CatchClause;ImportOrg.testng.annotations.BeforeMethod;Importorg.openqa.selenium.By;ImportOrg.openqa.selenium.JavascriptExecutor;ImportOrg.openqa.selenium.WebDriver;Importorg.openqa.selenium.WebElement;ImportOrg.openqa.selenium.chrome.ChromeDriver;ImportOrg.testng.annotations.AfterMethod; Public classScrollbarapi {webdriver driver; String BaseUrl; //Slide the scroll bar to the bottom of the page@Test (priority=0) Public voidf () {((javascriptexecutor) driver). Executescript ("Window.scrollto (0,document.body.scrollheight)"); Try{Thread.Sleep (3000); }Catch(interruptedexception e) {e.printstacktrace (); } } //slide to the specified element position@Test (Priority=1) Public voidScroll () {//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 (); } } //Slide the slider down 800 pixels@Test (priority=2) Public voidScrollto () {((javascriptexecutor) driver). Executescript ("Window.scrollby (0,800)"); Try{Thread.Sleep (3000); }Catch(interruptedexception e) {e.printstacktrace (); }} @BeforeMethod Public voidBeforemethod () {System.setproperty ("Webdriver.chrome.driver", "C:\\chromedriver\\chromedriver.exe"); Driver=NewChromedriver (); BASEURL= "Http://v.sogou.com"; Driver.navigate (). to (BASEURL); } @AfterMethod Public voidAftermethod () {Try{Thread.Sleep (3000);}Catch(interruptedexception e) {e.printstacktrace (); } driver.quit (); }}
Webdriver---API---(Java edition) Advanced applications 2