[英]Selenium - remember credentials using Java
因此,我在下面有一個簡單的Java Selenium腳本(適用於Chrome)。
我要做的就是:1.登錄網站A,並記住我的憑據(等同於在瀏覽器中彈出的“記住我”上單擊“是”)2.關閉硒3.手動並仍然打開瀏覽器正在登錄
有什么辦法嗎? 我已經在stackoverflow上看到了幾個類似的問題,但是並沒有設法使解決方案適用於我的情況。
幫助將不勝感激。
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class MyTestLogin {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "C://backup/Automation/drivers/chromedriver.exe");
WebDriver driver= new ChromeDriver();
//login to a website A
driver.get("https://websitea.com");
driver.findElement(By.name("username")).sendKeys("name.surname");
driver.findElement(By.name("password")).sendKeys("Huhuhu");
driver.findElement(By.className("flatbutton")).click();
driver.quit();
}
}
編輯:// Heureka!
經過StackOverflow的大量重寫和大量幫助之后,我終於能夠編寫一個腳本(並使用數組作為告訴他要復制哪些文件的方式),該腳本確實應有的作用-問題是,它不起作用。
當我執行以下操作時:
瀏覽器仍然不記得我。 但是,當我從步驟3手動復制備份的文件而不是步驟5時,它正在工作。
我想念什么?
import java.io.File;
import java.nio.file.Files;
import java.io.IOException;
import java.nio.file.StandardCopyOption;
import java.util.Arrays;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
public class MyTestLogin {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver", "C://backup/Automation/drivers/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
//Create a new temporary folder to store all the cookies and other website data
options.addArguments("user-data-dir=C://Users/user/Desktop/files/cookies");
WebDriver driver = new ChromeDriver(options);
//login to a website A
driver.get("https://websitea.com");
driver.findElement(By.name("username")).sendKeys("name.surname");
driver.findElement(By.name("password")).sendKeys("password");
driver.findElement(By.className("flatbutton")).click();
//new tab
//((JavascriptExecutor)driver).executeScript("window.open()");
//login to a website B
driver.get("https://websiteb.com");
driver.findElement(By.id("os_username")).sendKeys("name.surname");
driver.findElement(By.id("os_password")).sendKeys("password");
driver.findElement(By.id("loginButton")).click();
/*When you are finished logging in, close the browser
otherwise you´re unable to copy current session cookie */
driver.quit();
// now copy all the necessary files from a new folder to a browser´s cookie location
//Have a list of files that needs to be copied
java.util.List<String> names = Arrays.asList(
"Cookies",
"Cookies-journal",
"Current Session",
"Current Tabs",
"Favicons",
"Favicons-journal",
"History",
"History Provider Cache",
"History-journal",
"Last Session",
"Last Tabs",
"Login Data",
"Login Data-journal",
"Network Action Predictor",
"Network Action Predictor-journal",
"Network Persistent State",
"Origin Bound Certs",
"Origin Bound Certs-journal",
"Preferences",
"previews_opt_out.db",
"QuotaManager",
"QuotaManager-journal",
"Secure Preferences",
"Shortcuts",
"Shortcuts-journal",
"Top Sites",
"Top Sites-journal",
"Translate Ranker Model",
"TransportSecurity",
"Visited Links",
"Web Data",
"Web Data-journal"
);
// convert list to array
String[] namesArr = new String[names.size()];
namesArr = names.toArray(namesArr);
//iterate over array and...
int i=names.size();
int s=0;
while(i>=1){
//...for each item on the list, move it from one folder to another
System.out.println(namesArr[s]);
File sourceFile=new File ("C://Users/user/Desktop/Plocha/cookies/Default/"+namesArr[s]);
File destinationFile=new File("C://Users/user/AppData/Local/Google/Chrome/User Data/Default/"+namesArr[s]);
try{
Files.copy(sourceFile.toPath(),destinationFile.toPath(),StandardCopyOption.REPLACE_EXISTING);
System.out.println("File copy successfull");
} catch (IOException e) {
e.printStackTrace();
}
s++;
i--;
}
//now delete the temporary folder
File directory = new File("C://Users/user/Desktop/Plocha/cookies/");
//make sure directory exists
if(!directory.exists()){
System.out.println("Directory does not exist.");
System.exit(0);
}else{
try{
delete(directory);
}catch(IOException e){
e.printStackTrace();
System.exit(0);
}
}
System.out.println("Done");
}
public static void delete(File file)
throws IOException{
if(file.isDirectory()){
//directory is empty, then delete it
if(file.list().length==0){
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}else{
//list all the directory contents
String files[] = file.list();
for (String temp : files) {
//construct the file structure
File fileDelete = new File(file, temp);
//recursive delete
delete(fileDelete);
}
//check the directory again, if empty then delete it
if(file.list().length==0){
file.delete();
System.out.println("Directory is deleted : "
+ file.getAbsolutePath());
}
}
}else{
//if file, then delete it
file.delete();
System.out.println("File is deleted : " + file.getAbsolutePath());
}
}
}
創建一個空文件夾來存儲所有cookie和其他網站數據。 您可以通過設置user-data-dir參數告訴chrome webdriver將此文件夾用作文件夾。
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=/path/to/folder/");
WebDriver driver = new ChromeDriver(options);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.