最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java+Selenium實(shí)現(xiàn)文件上傳下載功能詳解

 更新時(shí)間:2023年01月09日 09:07:22   作者:洛陽(yáng)泰山  
這篇文章主要介紹了java代碼如何利用selenium操作瀏覽器上傳和下載文件功能,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以參考一下

簡(jiǎn)介

本文主要講解java代碼如何利用selenium操作瀏覽器上傳和下載文件代碼教程。

上傳文件

常見的 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è)是新打開的,所以要通過(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)文章

最新評(píng)論

景宁| 涞源县| 墨脱县| 德庆县| 东乡族自治县| 金沙县| 鹤山市| 黄山市| 朔州市| 霍州市| 嘉鱼县| 静宁县| 新源县| 漠河县| 嘉兴市| 漳州市| 平安县| 精河县| 平泉县| 迁安市| 吉隆县| 黄石市| 上饶市| 龙海市| 苏尼特左旗| 平遥县| 陕西省| 托克托县| 铁岭县| 山西省| 巴林右旗| 明水县| 鸡东县| 昭觉县| 昌图县| 桂平市| 北京市| 沅陵县| 靖远县| 庆城县| 乌拉特前旗|