Java+Selenium實(shí)現(xiàn)文件上傳下載功能詳解
簡(jiǎn)介
本文主要講解java代碼如何利用selenium操作瀏覽器上傳和下載文件代碼教程。
上傳文件
常見(jiàn)的 web 頁(yè)面的上傳,一般使用 input 標(biāo)簽或是插件(JavaScript、Ajax),對(duì)于 input 標(biāo)簽的上傳,可以直接使用 sendKeys(路徑) 來(lái)進(jìn)行上傳。
先寫一個(gè)測(cè)試用的頁(yè)面。
代碼如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <input type="file" name=""> </body> </html>
下面通過(guò) xpath 定位 input 標(biāo)簽,然后使用 sendKeys(filePath) 上傳文件。
import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.awt.*; import java.io.IOException; public class SeleniumDemo { private final static String webDriver = "webdriver.chrome.driver"; private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe"; public static void main(String[] args) throws InterruptedException, IOException, AWTException { System.setProperty(webDriver, webDriverPath); WebDriver driver= new ChromeDriver(); driver.get("file:///C:/Users/liuya/Desktop/test.html"); Thread.sleep(2000); String filePath="C:\\Users\\liuya\\Desktop\\doc\\tarzan.txt"; driver.findElement(By.xpath("http://*[@name='upload']")).sendKeys(filePath); } }
下載文件
Chrome瀏覽器
Firefox 瀏覽器要想實(shí)現(xiàn)文件下載,需要通過(guò) add_experimental_option 添加 prefs 參數(shù)。
download.default_directory:設(shè)置下載路徑。
profile.default_content_settings.popups:0 禁止彈出窗口。
import org.openqa.selenium.*; import org.openqa.selenium.chrome.ChromeDriver; import java.awt.*; import java.io.IOException; public class SeleniumDemo { private final static String webDriver = "webdriver.chrome.driver"; private final static String webDriverPath ="E:\\chromedriver\\chromedriver.exe"; public static void main(String[] args) throws InterruptedException, IOException, AWTException { System.setProperty(webDriver, webDriverPath); WebDriver driver= new ChromeDriver(); driver.get("http://pic.sogou.com/d?query=%E5%B0%8F%E7%8B%97&forbidqc=&entityid=&preQuery=&rawQuery=&queryList=&st=&did=45"); Thread.sleep(2000); driver.findElement(By.className("download")).click(); } }
當(dāng)你彈出像下面的頁(yè)面 “您的連接不是私密連接” 時(shí),可以直接鍵盤輸入 “thisisunsafe” 直接訪問(wèn)鏈接。那么這個(gè)鍵盤輸入字符串的操作就是之間講到的 sendKeys,但由于該標(biāo)簽頁(yè)是新打開(kāi)的,所以要通過(guò) switchTo().window() 將窗口切換到最新的標(biāo)簽頁(yè)。
//操作最新窗口 driver.switchTo().window(driver.getWindowHandles().stream().reduce((first, second) -> second).orElse(null)); driver.findElement(By.xpath("./html")).sendKeys("thisisunsafe");
到此這篇關(guān)于Java+Selenium實(shí)現(xiàn)文件上傳下載功能詳解的文章就介紹到這了,更多相關(guān)Java Selenium文件上傳下載內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)之二叉查找樹(shù)的實(shí)現(xiàn)
二叉查找樹(shù)(亦稱二叉搜索樹(shù)、二叉排序樹(shù))是一棵二叉樹(shù),且各結(jié)點(diǎn)關(guān)鍵詞互異,其中根序列按其關(guān)鍵詞遞增排列。本文將通過(guò)示例詳細(xì)講解二叉查找樹(shù),感興趣的可以了解一下2022-03-03springCloud服務(wù)注冊(cè)Eureka實(shí)現(xiàn)過(guò)程圖解
這篇文章主要介紹了springCloud服務(wù)注冊(cè)Eureka實(shí)現(xiàn)過(guò)程圖解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Java模擬QQ實(shí)現(xiàn)聊天互動(dòng)程序
這篇文章主要介紹了如何利用Java語(yǔ)言模擬QQ實(shí)現(xiàn)一個(gè)簡(jiǎn)易的聊天互動(dòng)程序,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-06-06java如何判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù))
這篇文章主要介紹了java如何判斷一個(gè)數(shù)是否是素?cái)?shù)(質(zhì)數(shù)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09Java 遍歷取出Map集合key-value數(shù)據(jù)的4種方法
這篇文章主要介紹了Java 遍歷取出Map集合key-value數(shù)據(jù)的4種方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09idea 訪問(wèn)html頁(yè)面端口號(hào)顯示的是63342而不是8080
這篇文章主要介紹了idea 訪問(wèn)html頁(yè)面端口號(hào)顯示的是63342而不是8080,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08