我webdriver的提供者接口:
package org.gk.networking;
public interface WebDriverProvider
{
WebDriver requestWebDriver(WebDriverInitialization aInitialization)
throws IllegalArgumentException,
WebDriverProviderException;
}
摘要實現:
package org.gk.networking;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.TargetLocator;
import org.openqa.selenium.WebElement;
public abstract class AbstractWebDriver implements WebDriver
{
AbstractWebDriver(org.openqa.selenium.WebDriver aWebDriver)
throws IllegalArgumentException
{
if(aWebDriver == null)
{
throw new IllegalArgumentException("aWebDriver may not be null");
}
this.mWebDriver = aWebDriver;
}
@Override
public void close()
throws Exception
{
this.mWebDriver.quit();
}
@Override
public final WebElement findElement(By aBy)
{
return this.mWebDriver.findElement(aBy);
}
@Override
public final List<WebElement> findElements(By aBy)
{
return this.mWebDriver.findElements(aBy);
}
@Override
public final void get(String aUrl)
{
this.mWebDriver.get(aUrl);
}
@Override
public final String getCurrentUrl()
{
return this.mWebDriver.getCurrentUrl();
}
@Override
public final String getPageSource()
{
return this.mWebDriver.getPageSource();
}
@Override
public final String getTitle()
{
return this.mWebDriver.getTitle();
}
@Override
public final String getWindowHandle()
{
return this.mWebDriver.getWindowHandle();
}
@Override
public final Set<String> getWindowHandles()
{
return this.mWebDriver.getWindowHandles();
}
@Override
public final Options manage()
{
return this.mWebDriver.manage();
}
@Override
public final Navigation navigate()
{
return this.mWebDriver.navigate();
}
@Override
public final void quit()
{
this.mWebDriver.quit();
}
@Override
public final TargetLocator switchTo()
{
return this.mWebDriver.switchTo();
}
@Override
public void webDriverClose()
{
this.mWebDriver.close();
}
@Override
public final org.openqa.selenium.WebDriver getWebDriver()
{
return this.mWebDriver;
}
private final org.openqa.selenium.WebDriver mWebDriver;
}
webdriver的接口,因爲它不是autocloseable,我不能延續原來,因爲它已經有一個close方法。
package org.gk.networking;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.TargetLocator;
public interface WebDriver extends AutoCloseable
{
WebElement findElement(By aBy);
List<WebElement> findElements(By aBy);
void get(String aUrl);
String getCurrentUrl();
String getPageSource();
String getTitle();
String getWindowHandle();
Set<String> getWindowHandles();
Options manage();
Navigation navigate();
void quit();
TargetLocator switchTo();
void webDriverClose();
org.openqa.selenium.WebDriver getWebDriver();
}
Tor的webdriver的實現: 包org.gk.networking;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TorWebDriver extends AbstractWebDriver
{
public TorWebDriver(FirefoxDriver aWebDriver)
throws IllegalArgumentException
{
super(aWebDriver);
}
}
webdriver的初始化接口: 包org.gk.networking;
import org.openqa.selenium.remote.DesiredCapabilities;
public interface WebDriverInitialization
{
DesiredCapabilities getCapabilities();
}
托爾webdriver的初始化執行: 包org.gk.networking;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.remote.DesiredCapabilities;
public class TorWebDriverInitialization implements WebDriverInitialization
{
public TorWebDriverInitialization(DesiredCapabilities aCapabilities, FirefoxProfile aProfile,
Collection<String> aCommandLineArguments)
throws IllegalArgumentException
{
if(aCapabilities == null)
{
throw new IllegalArgumentException("aCapabilities may not be null");
}
else if(aProfile == null)
{
throw new IllegalArgumentException("aProfile may not be null");
}
this.mCapabilities = aCapabilities;
this.mProfile = aProfile;
this.mCommandLineArguments = (aCommandLineArguments == null ? null : new ArrayList<>(aCommandLineArguments));
}
public TorWebDriverInitialization(FirefoxProfile aProfile)
throws IllegalArgumentException
{
this(new DesiredCapabilities(), aProfile, null);
}
public TorWebDriverInitialization()
throws IllegalArgumentException,
UnsupportedEncodingException,
FileNotFoundException
{
this(new DesiredCapabilities(), new FirefoxProfile(getDefaultProfileFile()), null);
}
@Override
public DesiredCapabilities getCapabilities()
{
return this.mCapabilities;
}
public FirefoxProfile getProfile()
{
return this.mProfile;
}
public Collection<String> getCommandLineArguments()
{
if(this.mCommandLineArguments == null)
{
return null;
}
else
{
return Collections.unmodifiableCollection(this.mCommandLineArguments);
}
}
public static synchronized File getDefaultProfileFile()
throws UnsupportedEncodingException,
FileNotFoundException
{
if(TOR_PROFILE_FILE != null)
{
return TOR_PROFILE_FILE;
}
String torProfilePath = "browsers/tor/profile.default";
URL resource = TorWebDriverInitialization.class.getClassLoader().getResource(torProfilePath);
if(resource == null)
{
throw new FileNotFoundException("Unable to open " + torProfilePath);
}
TOR_PROFILE_FILE = new File(URLDecoder.decode(resource.getFile(), "UTF-8"));
return TOR_PROFILE_FILE;
}
private final DesiredCapabilities mCapabilities;
private final FirefoxProfile mProfile;
private final Collection<String> mCommandLineArguments;
private static File TOR_PROFILE_FILE;
}
而在去年,該TOR webdriver的提供者實現: 包org.gk.networking;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLDecoder;
import java.util.Collection;
import org.apache.commons.lang3.SystemUtils;
import org.gk.PlatformNotSupportedException;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
public class TorWebDriverProvider implements WebDriverProvider
{
public TorWebDriverProvider()
throws FileNotFoundException,
UnsupportedEncodingException,
PlatformNotSupportedException
{
getTorBinaryFile();
}
@Override
public WebDriver requestWebDriver(WebDriverInitialization aInitialization)
throws IllegalArgumentException,
WebDriverProviderException
{
if(aInitialization == null)
{
throw new IllegalArgumentException("aInitialization may not be null");
}
else if(!(aInitialization instanceof TorWebDriverInitialization))
{
throw new IllegalArgumentException("aInitialization must be of type TorPhantomJSWebDriverInitialization");
}
TorWebDriverInitialization initialization = (TorWebDriverInitialization)aInitialization;
FirefoxBinary binary = null;
FirefoxDriver rawWebDriver = null;
try
{
binary = new FirefoxBinary(getTorBinaryFile());
Collection<String> commandLineArguments = initialization.getCommandLineArguments();
if(commandLineArguments != null)
{
for(String argument : commandLineArguments)
{
binary.addCommandLineOptions(argument);
}
}
rawWebDriver = new FirefoxDriver(binary, initialization.getProfile(), initialization.getCapabilities());
// Cheaty way to hide window
//rawWebDriver.manage().window().setSize(new Dimension(0, 0));
//rawWebDriver.manage().window().setPosition(new Point(-4000, 0));
return new TorWebDriver(rawWebDriver);
}
catch(Throwable t)
{
try
{
if(rawWebDriver != null)
{
rawWebDriver.quit();
}
}
catch(Throwable t2)
{
t.addSuppressed(t2);
}
try
{
if(binary != null)
{
binary.quit();
}
}
catch(Throwable t2)
{
t.addSuppressed(t2);
}
throw new WebDriverProviderException(t);
}
}
public static synchronized File getTorBinaryFile()
throws PlatformNotSupportedException,
FileNotFoundException,
UnsupportedEncodingException
{
if(TOR_BINARY_FILE != null)
{
return TOR_BINARY_FILE;
}
String torBinaryFilePath;
if(SystemUtils.IS_OS_WINDOWS)
{
if(SystemUtils.IS_OS_WINDOWS_NT || SystemUtils.IS_OS_WINDOWS_95 || SystemUtils.IS_OS_WINDOWS_98
|| SystemUtils.IS_OS_WINDOWS_ME || SystemUtils.IS_OS_WINDOWS_2000)
{
throw new PlatformNotSupportedException("Minimum Windows version required is XP");
}
torBinaryFilePath = "browsers/tor/windows/tor_windows32/firefox.exe";
}
else if(SystemUtils.IS_OS_MAC_OSX)
{
if(SystemUtils.IS_OS_MAC_OSX_CHEETAH || SystemUtils.IS_OS_MAC_OSX_PUMA || SystemUtils.IS_OS_MAC_OSX_JAGUAR
|| SystemUtils.IS_OS_MAC_OSX_PANTHER || SystemUtils.IS_OS_MAC_OSX_TIGER
|| SystemUtils.IS_OS_MAC_OSX_LEOPARD)
{
throw new PlatformNotSupportedException("Minimum OSX version required is Snow Leopard");
}
torBinaryFilePath = "browsers/tor/osx/tor_osx64.dmg";
}
else if(SystemUtils.IS_OS_LINUX)
{
String architecture = SystemUtils.OS_ARCH;
if(architecture == null)
{
throw new SecurityException("No permissions to read system property os.arch");
}
if(architecture.contains("64"))
{
torBinaryFilePath = "browsers/tor/linux/tor_linux32/firefox";
}
else
{
torBinaryFilePath = "browsers/tor/linux/tor_linux64/firefox";
}
}
else
{
throw new PlatformNotSupportedException("The current platform is not supported");
}
URL resource = TorWebDriverProvider.class.getClassLoader().getResource(torBinaryFilePath);
if(resource == null)
{
throw new FileNotFoundException("Unable to open " + torBinaryFilePath);
}
TOR_BINARY_FILE = new File(URLDecoder.decode(resource.getFile(), "UTF-8"));
return TOR_BINARY_FILE;
}
private static File TOR_BINARY_FILE;
}
希望這對你有用。
編輯: 順便說一句,該配置文件是附帶Tor瀏覽器副本的文件夾。只需在下載時搜索它。而二進制文件就是例如windows上的firefox.exe。
使用FirefoxDriver與Tor瀏覽器一起。您可以將終端節點設置爲特定的國家/地區。由於Tor瀏覽器基本上是Firefox – Neijwiert
哦,等待會影響加載時間很多..嗯很難測試實際。即使使用VPN連接,它仍然會變慢。 – Neijwiert
我不認爲有任何'測試加載時間'的可靠方法,這是非常可變的,即使你在國內本身,它可能會更快/更慢,如果你做同樣的事情,例如10個街區。 – Neijwiert