无法使用Selenium java在Chrome浏览器中下载pdf文件
我的使用案例:我必须从pdf中读取数据,然后在chrome浏览器中打开,并检查pdf中是否存在某些特定数据 由于我无法实现上述目标,我想在我的电脑上下载文件,并使用PDFbox进行验证。 我创建了一个带有设置的chrome配置文件,可以直接下载pdf文件(设置>内容设置>pdf文档)。 我在selenium脚本中将其设置为chrome选项。 测试工作正常,但当pdf打开时,它不会开始下载。 PDF文件将在我的chrome浏览器中打开。Chrome版本:62.0.3202.94 我从以下位置获取chrome配置文件路径:无法使用Selenium java在Chrome浏览器中下载pdf文件,java,google-chrome,selenium,pdf,Java,Google Chrome,Selenium,Pdf,我的使用案例:我必须从pdf中读取数据,然后在chrome浏览器中打开,并检查pdf中是否存在某些特定数据 由于我无法实现上述目标,我想在我的电脑上下载文件,并使用PDFbox进行验证。 我创建了一个带有设置的chrome配置文件,可以直接下载pdf文件(设置>内容设置>pdf文档)。 我在selenium脚本中将其设置为chrome选项。 测试工作正常,但当pdf打开时,它不会开始下载。 PDF文件将在我的chrome浏览器中打开。Chrome版本:62.0.3202.94 我从以下位置获取c
chrome://version/
我不确定出了什么问题。
请帮忙
@Before
public void beforeTest() throws MalformedURLException{
System.setProperty("webdriver.chrome.driver","path to chromedriver\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
String chromeProfilePath="path to custom chrome profile";
options.addArguments("user-data-dir="+chromeProfilePath);
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
//Browser is maximized
driver.manage().window().maximize();
}
@之前
public void beforeTest()引发了错误的FormedUrlexception{
System.setProperty(“webdriver.chrome.driver”,“chromedriver\\chromedriver.exe的路径”);
ChromeOptions选项=新的ChromeOptions();
字符串chromeProfilePath=“自定义chrome配置文件的路径”;
options.addArguments(“用户数据目录=”+chromeProfilePath);
HashMap chromeOptionsMap=新HashMap();
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY,ChromeOptions sMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
驱动器=新的镀铬驱动器(cap);
//浏览器最大化
driver.manage().window().maximize();
}
您应该禁用pdf查看器插件,以禁止在chrome中打开pdf文件。添加此chrome选项
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[] {
"Chrome PDF Viewer"
});
// launch the browser and navigate to the page
ChromeDriver driver = new ChromeDriver(options);
ChromeOptions选项=新的ChromeOptions();
映射首选项=新哈希表();
选项。设置实验选项(“首选项”,首选项);
//禁用flash和PDF查看器
preferences.put(“plugins.plugins_disabled”),新字符串[]{
“Chrome PDF查看器”
});
//启动浏览器并导航到该页面
ChromeDriver驱动程序=新的ChromeDriver(选项);
您应该禁用pdf查看器插件,以禁止在chrome中打开pdf文件。添加此chrome选项
ChromeOptions options = new ChromeOptions();
Map<String, Object> preferences = new Hashtable<String, Object>();
options.setExperimentalOption("prefs", preferences);
// disable flash and the PDF viewer
preferences.put("plugins.plugins_disabled", new String[] {
"Chrome PDF Viewer"
});
// launch the browser and navigate to the page
ChromeDriver driver = new ChromeDriver(options);
ChromeOptions选项=新的ChromeOptions();
映射首选项=新哈希表();
选项。设置实验选项(“首选项”,首选项);
//禁用flash和PDF查看器
preferences.put(“plugins.plugins_disabled”),新字符串[]{
“Chrome PDF查看器”
});
//启动浏览器并导航到该页面
ChromeDriver驱动程序=新的ChromeDriver(选项);
检查此选项下载pdf非常有效
package testing;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class pdfdownload {
static String urls ="http://www.staff.amu.edu.pl/~zcht/pliki/Databases%20for%20beginners.pdf";
public static void main(String[] args) throws IOException {
URL url = verify(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf('/')+1);
FileOutputStream outputStream = new FileOutputStream("D:\\HELLO/java" + File.separator+ filename);
inputStream = connection.getInputStream();
int read = -1;
byte[] buffer = new byte[4096];
while((read = inputStream.read(buffer))!= -1){
outputStream.write(buffer,0,read);
}
inputStream.close();
outputStream.close();
}
private static URL verify(String url){
if(!url.toLowerCase().startsWith("http://")){
return null;
}
URL verifyURL= null;
try{
verifyURL = new URL(url);
}catch(Exception e){
}
return verifyURL;
}}
要验证pdf内容,请使用此
package pdf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.testng.Assert;
public class pdfreader {
public static void main(String[] args) throws IOException {
File file = new File("D://study video tutorials//database testing//Database Testing Quick Guide.pdf");
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
COSDocument cosDoc= parser.getDocument();
PDDocument pddoc= new PDDocument(cosDoc);
PDFTextStripper strip= new PDFTextStripper();
String data = strip.getText(pddoc);
System.out.println(data);
Assert.assertTrue(data.contains("keys"));
cosDoc.close();
pddoc.close();
}
}
检查此项,下载pdf非常有效
package testing;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class pdfdownload {
static String urls ="http://www.staff.amu.edu.pl/~zcht/pliki/Databases%20for%20beginners.pdf";
public static void main(String[] args) throws IOException {
URL url = verify(urls);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream inputStream = null;
String filename = url.getFile();
filename = filename.substring(filename.lastIndexOf('/')+1);
FileOutputStream outputStream = new FileOutputStream("D:\\HELLO/java" + File.separator+ filename);
inputStream = connection.getInputStream();
int read = -1;
byte[] buffer = new byte[4096];
while((read = inputStream.read(buffer))!= -1){
outputStream.write(buffer,0,read);
}
inputStream.close();
outputStream.close();
}
private static URL verify(String url){
if(!url.toLowerCase().startsWith("http://")){
return null;
}
URL verifyURL= null;
try{
verifyURL = new URL(url);
}catch(Exception e){
}
return verifyURL;
}}
要验证pdf内容,请使用此
package pdf;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdfparser.PDFParser;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.util.PDFTextStripper;
import org.testng.Assert;
public class pdfreader {
public static void main(String[] args) throws IOException {
File file = new File("D://study video tutorials//database testing//Database Testing Quick Guide.pdf");
FileInputStream fis = new FileInputStream(file);
PDFParser parser = new PDFParser(fis);
parser.parse();
COSDocument cosDoc= parser.getDocument();
PDDocument pddoc= new PDDocument(cosDoc);
PDFTextStripper strip= new PDFTextStripper();
String data = strip.getText(pddoc);
System.out.println(data);
Assert.assertTrue(data.contains("keys"));
cosDoc.close();
pddoc.close();
}
}
我可以在Chrome中下载pdf,而无需创建新的用户配置文件。我想如果有人想要类似的答案,我可以把它贴在这里:
@Before
public void beforeTest() throws Exception{
System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
chromeOptionsMap.put("plugins.plugins_disabled", new String[] {
"Chrome PDF Viewer"
});
chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", chromeOptionsMap);
String downloadFilepath = "download folder path";
chromeOptionsMap.put("download.default_directory", downloadFilepath);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
//Browser is maximized
driver.manage().window().maximize();
//Browser navigates to the url
driver.navigate().to("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@之前
public void beforeTest()引发异常{
System.setProperty(“webdriver.chrome.driver”,“chromedriver.exe的路径”);
ChromeOptions选项=新的ChromeOptions();
HashMap chromeOptionsMap=新HashMap();
chromeOptionsMap.put(“plugins.plugins_disabled”),新字符串[]{
“Chrome PDF查看器”
});
chromeOptionsMap.put(“plugins.always\u open\u pdf\u externally”,true);
选项。设置实验选项(“prefs”,chromeOptionsMap);
String downloadFilepath=“下载文件夹路径”;
chromeOptionsMap.put(“download.default_目录”,downloadFilepath);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY,ChromeOptions sMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
驱动器=新的镀铬驱动器(cap);
//浏览器最大化
driver.manage().window().maximize();
//浏览器导航到url
driver.navigate()指向(“URL”);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
我可以在Chrome中下载pdf,而无需创建新的用户配置文件。我想如果有人想要类似的答案,我可以把它贴在这里:
@Before
public void beforeTest() throws Exception{
System.setProperty("webdriver.chrome.driver","path to chromedriver.exe");
ChromeOptions options = new ChromeOptions();
HashMap<String, Object> chromeOptionsMap = new HashMap<String, Object>();
chromeOptionsMap.put("plugins.plugins_disabled", new String[] {
"Chrome PDF Viewer"
});
chromeOptionsMap.put("plugins.always_open_pdf_externally", true);
options.setExperimentalOption("prefs", chromeOptionsMap);
String downloadFilepath = "download folder path";
chromeOptionsMap.put("download.default_directory", downloadFilepath);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY, chromeOptionsMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(cap);
//Browser is maximized
driver.manage().window().maximize();
//Browser navigates to the url
driver.navigate().to("URL");
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@之前
public void beforeTest()引发异常{
System.setProperty(“webdriver.chrome.driver”,“chromedriver.exe的路径”);
ChromeOptions选项=新的ChromeOptions();
HashMap chromeOptionsMap=新HashMap();
chromeOptionsMap.put(“plugins.plugins_disabled”),新字符串[]{
“Chrome PDF查看器”
});
chromeOptionsMap.put(“plugins.always\u open\u pdf\u externally”,true);
选项。设置实验选项(“prefs”,chromeOptionsMap);
String downloadFilepath=“下载文件夹路径”;
chromeOptionsMap.put(“download.default_目录”,downloadFilepath);
DesiredCapabilities=DesiredCapabilities.chrome();
cap.setCapability(ChromeOptions.CAPABILITY,ChromeOptions sMap);
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS,true);
上限设置能力(色度选项、能力、选项);
驱动器=新的镀铬驱动器(cap);
//浏览器最大化
driver.manage().window().maximize();
//浏览器导航到url
driver.navigate()指向(“URL”);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
只需添加类型注册表
[HKEY\U LOCAL\U MACHINE\Software\Policys\Google\Chrome]
“alwaysopenpdfexternaly”=dword:00000001只需添加类型注册表 [HKEY\U LOCAL\U MACHINE\Software\Policys\Google\Chrome] “Alwaysopenpdfexternaly”=dword:00000001使用Chrome选项,
禁用插件-Chrome PDF Viewer,
启用插件-始终在外部打开\u pdf\u,
设置自己的下载路径-下载。默认目录