2017-06-02 26 views
11

我正在爬行一個安全的網站,該網站在我重新啓動我的爬蟲程序(我需要將IP作爲技巧)時會阻止它。我在這樣的鍍鉻驅動程序使用默認的用戶配置文件解決了這個問題(我使用C#的權利,但如果需要的話我可以切換到Java):在每次運行時爲PhantomJs使用相同的會話

ChromeOptions options = new ChromeOptions(); 
options.AddArguments($"user-data-dir=C:/Users/{Environment.UserName}/AppData/Local/Google/Chrome/User Data/Default"); 

它保存所有的會議和餅乾以及何時恢復它們重啓我的應用程序。一切按預期工作。

現在,由於某些原因,我需要將我的webdriver更改爲PhantomJS。

我的問題我怎樣才能使使用PhantomJS這種情況下可能:登錄帳戶(如Gmail或Facebook),請關閉我的應用程序和驅動程序,發現自己登錄我下次運行應用程序,驅動程序。換句話說,我如何在每次運行中爲PhantomJS使用相同的會話?

嘗試1(在C#):

做一些搜索後,我發現,這可以通過使用本地存儲和PhantomJS的cookies文件的參數來完成。現在問題是本地存儲路徑總是空的,沒有任何東西被保存(我導航到多個網站,但仍然是空的),因此,我不能使用先前執行的會話。我的代碼來設置本地存儲和cookie文件很簡單,如下:

PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService(); 
service.LocalStoragePath = Application.StartupPath + "\\default"; 
service.CookiesFile = Application.StartupPath + "\\default\\Cookies"; 
IWebDriver driver = new PhantomJSDriver(service); 

什麼是錯我的做法?

嘗試2(在C#):

基於@SiKing回答和評論的討論,我改成下面的代碼(使用AddArgument),但該目錄仍然是空的:

string localStoragePath = Path.Combine(Path.GetTempPath(),"PhantomLocalStorage-"); 

if (!Directory.Exists(localStoragePath)) 
{ 
    Directory.CreateDirectory(localStoragePath); 
} 

PhantomJSDriverService service = PhantomJSDriverService.CreateDefaultService(); 
service.AddArgument("--local-storage-quota=5000"); 
service.AddArgument("--local-storage-path=" + localStoragePath); 
IWebDriver driver = new PhantomJSDriver(service); 

嘗試3(在Java中):

目錄仍然是空的:

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); 
List<String> cliArgs = new ArrayList<String>(); 
Path local_storage_path = Paths.get(System.getProperty("java.io.tmpdir") + "PhantomLocalStorage-"); 
if (Files.notExists(local_storage_path)) { 
    try { 
     Files.createDirectory(local_storage_path); 
    } 
    catch (IOException e) { 
     JOptionPane.showConfirmDialog(null, "Can Not Create Path"); 
    } 
} 
cliArgs.add("--local-storage-quota=5000"); 
cliArgs.add("--local-storage-path=" + local_storage_path.toString()); 
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs); 
WebDriver driver = new PhantomJSDriver(capabilities); 
+0

目前還不清楚,「這種行爲」究竟是什麼意思。 使用PhantomJS,您可以保存和恢復Cookie,本地存儲,WebSQL內容:http://phantomjs.org/api/command-line.html –

+0

這意味着在瀏覽過程中將所有用戶數據保存到目錄,並從所需的目錄恢復它們啓動時的目錄。例如:使用由簡單的.net應用程序控制的phantomjs Web驅動程序登錄到gmail帳戶,然後關閉應用程序和Web驅動程序(不註銷Gmail)。下次啓動應用程序(也稱爲webdriver)並導航到https://www.gmail.com時,您應該已經登錄。這是通過配置文件啓動時chrome的默認行爲,就像上面的代碼一樣。正如鏈接所說,我已經測試了cookie文件,但並沒有爲我工作。 – Efe

回答

3

PhantomJS默認啓動時沒有本地存儲;見this discussion

要通過Selenium啓用本地存儲,我使用了以下Java代碼。對不起,從我使用C#開始已經太久了,但我相信C#綁定有類似的方法可用。

DesiredCapabilities capabilities = DesiredCapabilities.phantomjs(); 
// Phantom options can only be set from CLI 
List<String> cliArgs = new ArrayList<String>(); 
cliArgs.add("--local-storage-quota=5000"); 
Path local_storage_path = Files.createTempDirectory("PhantomLocalStorage-"); 
cliArgs.add("--local-storage-path=" + local_storage_path.toString()); 
capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cliArgs); 
WebDriver driver = new PhantomJSDriver(capabilities); 

注意,你用它做後local_storage_path不會被刪除。如果您需要,可以按照this post設置掛鉤。但我懷疑在C#中這部分將會與Java大相徑庭。

+0

我有一個問題。因爲從來沒有用戶期望的功能,我如何將cliArgs添加到功能並將其傳遞給驅動程序? – Efe

+0

這裏在C#中,我們有功能對象的SetCapability方法如下:SetCapability(字符串功能,對象capabilityValue)。這是你添加到cliArgs列表的名字和價值嗎?如果是這樣,我如何設置PhantomJsDriver的功能? – Efe

+1

@Efe我在示例代碼示例中添加了兩行。對於那個很抱歉。 – SiKing

1

對於餅乾部分,你可以只保存從一個會話:

ReadOnlyCollection<Cookie> cookiesFromFirstSession = driver.Manage().Cookies.AllCookies; 

並將其加載到另一個webdriver的會話:

driver.Manage().Cookies.AddCookie(); 

我猜想,這是餅乾,不一定任何其他本地存儲數據,您需要這些數據才能保持相同的會話。

+0

我已經做到了這一點,但每次我都會得到新的會話。我也從chromedriver(保留會話)獲取coockies並將它們設置爲PhantomJs,但仍然是相同的 – Efe

相關問題