Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Mac上使用selenium Chromedriver 3.11.0自动下载文件_Java_Selenium_Selenium Webdriver_Selenium Chromedriver_Mutablecapabilities - Fatal编程技术网

Java 在Mac上使用selenium Chromedriver 3.11.0自动下载文件

Java 在Mac上使用selenium Chromedriver 3.11.0自动下载文件,java,selenium,selenium-webdriver,selenium-chromedriver,mutablecapabilities,Java,Selenium,Selenium Webdriver,Selenium Chromedriver,Mutablecapabilities,我已经在这里阅读了一系列关于selenium的主题,并且一直在讨论如何为chromedriver设置权限/选项。我编写了以下代码: System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver"); String downloadFilepath = "//User//username//automation-testing//"; HashMap<String, Object> chromeP

我已经在这里阅读了一系列关于selenium的主题,并且一直在讨论如何为chromedriver设置权限/选项。我编写了以下代码:

System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);
它重定向到csv文件


我一直收到一个弹出提示,问我下载文件是否可以。值得一提的是,新的ChromeDriver(cap)行已被弃用,但我似乎找不到关于如何使用替代它的方法的文档,该方法涵盖了这个用例。

您似乎就快到了。您需要使用类中的方法将DesiredCapabilities类型的对象合并为ChromeOptions类型的对象,并通过传递ChromeOptions对象来启动WebDriver和WebClient实例,如下所示:

System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(cap);
WebDriver driver = new ChromeDriver(options);
System.setProperty(“webdriver.chrome.driver”,“/Users/username/chromedriver”);
String downloadFilepath=“//用户//用户名//自动测试/”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“禁用弹出窗口阻止”);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
期权。合并(cap);
WebDriver=新的ChromeDriver(选项);
PS:作为参考,您可以查看tag中的讨论


更新
根据您的评论更新,当您被“下载确认”窗口卡住时,您可以查看讨论以解决您的问题。

您似乎就快到了。您需要使用类中的方法将DesiredCapabilities类型的对象合并为ChromeOptions类型的对象,并通过传递ChromeOptions对象来启动WebDriver和WebClient实例,如下所示:

System.setProperty("webdriver.chrome.driver", "/Users/username/chromedriver");
String downloadFilepath = "//User//username//automation-testing//";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
options.addArguments("disable-popup-blocking");
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
options.merge(cap);
WebDriver driver = new ChromeDriver(options);
System.setProperty(“webdriver.chrome.driver”,“/Users/username/chromedriver”);
String downloadFilepath=“//用户//用户名//自动测试/”;
HashMap chromePrefs=新HashMap();
chromePrefs.put(“profile.default\u content\u settings.popups”,0);
chromePrefs.put(“download.default\u目录”,downloadFilepath);
ChromeOptions选项=新的ChromeOptions();
选项。设置实验选项(“prefs”,chromePrefs);
options.addArguments(“禁用弹出窗口阻止”);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
期权。合并(cap);
WebDriver=新的ChromeDriver(选项);
PS:作为参考,您可以查看tag中的讨论


更新
根据您的评论更新,当您被“下载确认”窗口卡住时,您可以查看讨论来解决您的问题。

我使用的解决方案非常丰富,下面是完整的源代码,通过下面的谷歌登录jira,然后将过滤器视图下载到下面的csv(当前选择):

import com.fasterxml.jackson.core.JsonProcessingException;
导入com.fasterxml.jackson.databind.ObjectMapper;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.entity.StringEntity;
导入org.apache.http.impl.client.CloseableHttpClient;
导入org.apache.http.impl.client.HttpClients;
导入org.openqa.selenium.By;
导入org.openqa.selenium.chrome.ChromeDriver;
导入org.openqa.selenium.chrome.ChromeDriverService;
导入org.openqa.selenium.chrome.ChromeOptions;
导入java.io.File;
导入java.io.FileFilter;
导入java.io.IOException;
导入java.io.UnsupportedEncodingException;
导入java.util.HashMap;
导入java.util.Map;
公费舱{
公共静态void doScrape(字符串[]URL){
试一试{
for(字符串url:url){
//创建新的Chromedriver,设置文件下载路径,允许自动接受下载弹出窗口,并将属性合并到Chromedriver中
System.setProperty(“webdriver.chrome.driver”,“/Users/damienbell/chromedriver”);
字符串downloadFilepath=“/Users/damienbell/automation testing”;
ChromeOptions选项=新的ChromeOptions();
options.addArguments(“--test type”);
//选项。添加参数(“--headless”);
options.addArguments(“--disable extensions”);
//在driverService中实例化上述选项
ChromeDriverService driverService=ChromeDriverService.createDefaultService();
ChromeDriver驱动程序=新的ChromeDriver(驱动服务,选项);
Map commandParams=new HashMap();
commandParams.put(“cmd”,“Page.setDownloadBehavior”);
Map params=新的HashMap();
参数put(“行为”、“允许”);
参数put(“downloadPath”,downloadFilepath);
参数put(“cmd”,“Page.setDownloadBehavior”);
commandParams.put(“params”,params);
ObjectMapper om=新的ObjectMapper();
CloseableHttpClient httpClient=HttpClients.createDefault();
字符串命令=null;
试一试{
command=om.writeValueAsString(commandParams);
}catch(JsonProcessingException jpe){jpe.printStackTrace();}
字符串postrl=driverService.getUrl().toString()+“/session/”+driver.getSessionId()+“/chromium/send_command”;
HttpPostRequest=新的HttpPost(postrl);
addHeader(“内容类型”、“应用程序/json”);
addHeader(“接受”、“**”);
试一试{
setEntity(新的StringEntity(命令));
httpClient.execute(postRequest);
}
catch(UnsupportedEncodingException uee){uee.printStackTrace();}
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.chrome.ChromeOptions;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public class Scrape {
    public static void doScrape(String[] urls) {
        try{
            for(String url : urls) {
                //Create new Chromedriver, set file download path, allow the download popup to be automatically accepted,and merge the properties into chromedriver
                System.setProperty("webdriver.chrome.driver", "/Users/damienbell/chromedriver");
                String downloadFilepath = "/Users/damienbell/automation-testing";

                ChromeOptions options = new ChromeOptions();
                options.addArguments("--test-type");
                //options.addArguments("--headless");
                options.addArguments("--disable-extensions");

                //Instantiate above options in driverService
                ChromeDriverService driverService = ChromeDriverService.createDefaultService();
                ChromeDriver driver = new ChromeDriver(driverService, options);


                Map<String, Object> commandParams = new HashMap<>();
                commandParams.put("cmd", "Page.setDownloadBehavior");

                Map<String, Object> params = new HashMap<String, Object>();
                params.put("behavior", "allow");
                params.put("downloadPath", downloadFilepath);
                params.put("cmd", "Page.setDownloadBehavior");


                commandParams.put("params", params);
                ObjectMapper om = new ObjectMapper();
                CloseableHttpClient httpClient = HttpClients.createDefault();
                String command = null;
                try{
                    command = om.writeValueAsString(commandParams);
                }catch(JsonProcessingException jpe){ jpe.printStackTrace(); }
                String postURL = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
                HttpPost postRequest = new HttpPost(postURL);
                postRequest.addHeader("content-type", "application/json");
                postRequest.addHeader("accept", "*.*");
                try{
                    postRequest.setEntity(new StringEntity(command));
                    httpClient.execute(postRequest);
                }
                catch (UnsupportedEncodingException uee) { uee.printStackTrace(); }
                catch (IOException ioe) { ioe.printStackTrace(); }


                driver.get("https://x.atlassian.net/secure/Dashboard.jspa?selectPageId=11502");
                Thread.sleep(3000);  // Let the user actually see something!
                ((ChromeDriver) driver).findElementById("menu-sign-in").click();
                Thread.sleep(3000);
                ((ChromeDriver) driver).findElementById("google-signin-button").click();
                Thread.sleep(3000);
                ((ChromeDriver) driver).findElementById("identifierId").sendKeys("email@email.com");
                Thread.sleep(700);
                ((ChromeDriver) driver).findElementById("identifierNext").click();
                Thread.sleep(2000);
                driver.findElement(By.cssSelector("input[name=password]")).sendKeys(Secret.getPassword());
                Thread.sleep(600);
                ((ChromeDriver) driver).findElementById("passwordNext").click();
                Thread.sleep(10000);
                driver.get(url);
                Thread.sleep(56000);
                File[] files = new File("/Users/user/automation-testing").listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File path) {
                        if (path.isFile()) {
                            ParseCSV.doParse(path);
                            path.delete();
                            return true;
                        }
                        else{
                            System.out.println("Failure");
                        }
                        return false;
                    }
                });
                driver.quit();
            }
        }catch( java.lang.InterruptedException inter ){ System.err.println("Thread.sleep broke something, wtf"); inter.printStackTrace(); }
    }
}
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('headless')