I'm trying to use my Socks5 proxy while using Selenium web driver here is my code :
System.setProperty("webdriver.chrome.driver", "dependencies/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
Proxy prx =new Proxy() ;
prx.setSocksVersion(5);
prx.setSocksProxy("xx.xxx.xxx.xxx:8000");
prx.setSocksPassword("5xxxxS") ;
prx.setSocksUsername("VxxxxW") ;
options.addArguments("--disable-notifications");
options.addArguments("start-minimized");
options.setCapability("proxy", prx);
WebDriver driver = new ChromeDriver(options); // issue here
I'm having the following error :
Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.
You should add proxy server arguments to ChromeOptions:
String host = "shadowsocks6.freeproxy.center";
String port = "8989";
String socks5User = "SOCKS_USER";
String socks5Pass = "SOCKS_Pass";
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--proxy-server=socks5://" + host + ":" + port);
chromeOptions.addArguments("--proxy-auth=" + socks5User + ":" + socks5Pass);
WebDriver driver = new ChromeDriver(chromeOptions);
You can refer to selenium-tiny-projects for complete source code
Related
Hi I am trying to add some settings to my driver in selenium but can seem to make them work.
Im trying to add
{
"DEVELOPER_MODE":false,
"AUTOMATION_MODE":true,
"AUTOMATION_LAUNCH_URL":"{whatever_url_you_want_to_test_with}"
}
to my driver file
public static WebDriver startDriverTwo() {
String projectLocation = System.getProperty("user.dir");
System.setProperty("webdriver.chrome.driver", projectLocation + "/chromedriver.exe");
ChromeOptions opt = new ChromeOptions();
opt.addArguments("start-maximized");
opt.addArguments("disable-infobars");
opt.addArguments("--disable-extensions");
opt.addArguments("--window-size=1920x1080");
opt.addArguments("--disable-cache");
//options.addArguments("--headless");
opt.addArguments("--disable-application-cache");
opt.addArguments("--disk-cache-size=0");
opt.addArguments("--disable-gpu"); // applicable to windows os only
opt.addArguments("--disable-dev-shm-usage"); // overcome limited resource problems
opt.addArguments("--dns-prefetch-disable");
opt.addArguments("--disable-notifications");
opt.addArguments("disable-infobars")
//Enter the path of your Electron app
opt.setBinary("pathtoelectronapp.exe");
driver = new ChromeDriver(opt);
System.out.println("opening app");
return driver;
}
I have tried
setExperimentalOption()
setProperty()
But neither of these work? any idea how i can work these in?
DEVELOPER_MODE - you got that right, it's opt.addArguments("--disable-extensions");
I'm little bit confused with these settings, since:
AUTOMATION_MODE - isn't this always true, when using Selenium?
AUTOMATION_LAUNCH_URL - you mean driver.get(URL) ?
I am using selenium for a test in a project, but I have a problem.
I need to get network files from google chrome when I inspect element.
In this section I need this files, they are JSON files, and I need its information.
//String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String scriptToExecute = "var network = performance.getEntries() || {}; return network;";
java.util.List<String> s= executeJavaScript(scriptToExecute)
String s attribute, return me a strange List of strange objects of the network, isn't good information for me.
This is my problem, I need JSON files, but my code returns me other things.
Use BrowserMobProxyServer along with selenium to get the network details of each network HAR format.
// Set up BrowserMobProxyServer while initiation driver
proxy = new BrowserMobProxyServer();
proxy.start(0);
Proxy seleniumProxy = ClientUtil.createSeleniumProxy(proxy);
// configure it as a desired capability
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.PROXY, seleniumProxy);
driver = new ChromeDriver(capabilities);
proxy.enableHarCaptureTypes(CaptureType.REQUEST_CONTENT, CaptureType.RESPONSE_CONTENT);
proxy.newHar("google.com");
// Do all your navigation in selenium/ selenide code
driver.get("http://google.com");
// After navigation, you can find network details stored HAR
Har har = proxy.getHar();
If required you can store it to file before quiting the driver,
Har har = proxy.getHar();
File harFile = new File(sFileName);
har.writeTo(harFile);
proxy.stop();
driver.quit();
1.Create a webdriver with capabilities to monitor network calls too.
public static WebDriver getDriver() {
ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.driver", Driver local path);
DesiredCapabilities cap = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
cap.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
Map<String, Object> perfLogPrefs = new HashMap<String, Object>();
perfLogPrefs.put("traceCategories", "browser,devtools.timeline,devtools"); // comma-separated trace categories
options.setExperimentalOption("perfLoggingPrefs", perfLogPrefs);
cap.setCapability(ChromeOptions.CAPABILITY, options);
return new ChromeDriver(cap);
}
Now retrieve your log entries using following code.
for (LogEntry entry : driver.manage().logs().get(LogType.PERFORMANCE)){
System.out.println(entry.toString());
}
3.Result will be in following JSON format.
{
"webview": <originating WebView ID>,
"message": { "method": "...", "params": { ... }} // DevTools message.
}
You can use return JSON.stringify(network) replace return network.Then executeJavaScript(scriptToExecute) will return json,but it still String If you don't change to json by java.You can use org.json.Just
JSONArray netData = new JSONArray(driver.executeScript(scriptToExecute).toString());
As it is still not clear for me how to download files using --headless mode in ChromeDriver - selenium [Java], add here please the example of how to do so, I try to do it like that (the file downloading works properly without --headless option):
ChromeOptions lChromeOptions = new ChromeOptions();
HashMap<String, Object> lChromePrefs = new HashMap<String, Object>();
lChromePrefs.put("profile.default_content_settings.popups", 0);
lChromePrefs.put("download.default_directory", _PATH_TO_DOWNLOAD_DIR);
lChromePrefs.put("browser.set_download_behavior", "{ behavior: 'allow' , downloadPath: '"+_PATH_TO_DOWNLOAD_DIR+"'}");
lChromeOptions.addArguments("--headless");
lChromeOptions.addArguments("--disable-gpu");
lChromeOptions.setExperimentalOption("prefs", lChromePrefs);
WebDriver lWebDriver = new ChromeDriver(lChromeOptions);
From what I know, downloading files in headless mode should be possible since Chrome v60+ by setting Browser.setDownloadBehaviour(true, _DIRECTORY) but I cant find the information whether ChromeDriver already supports it or it is just me using wrong chrome preferences as arguments
ChromeDriver version: 2.34
Selenium + WebDriver version: 3.8.1
In Java use like this :
System.setProperty("webdriver.chrome.driver", "/usr/local/bin/chromedriver");
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
options.addArguments("--headless");
options.addArguments("--disable-extensions"); //to disable browser extension popup
ChromeDriverService driverService = ChromeDriverService.createDefaultService();
ChromeDriver driver = new ChromeDriver(driverService, options);
Map<String, Object> commandParams = new HashMap<>();
commandParams.put("cmd", "Page.setDownloadBehavior");
Map<String, String> params = new HashMap<>();
params.put("behavior", "allow");
params.put("downloadPath", "//home//vaibhav//Desktop");
commandParams.put("params", params);
ObjectMapper objectMapper = new ObjectMapper();
HttpClient httpClient = HttpClientBuilder.create().build();
String command = objectMapper.writeValueAsString(commandParams);
String u = driverService.getUrl().toString() + "/session/" + driver.getSessionId() + "/chromium/send_command";
HttpPost request = new HttpPost(u);
request.addHeader("content-type", "application/json");
request.setEntity(new StringEntity(command));
httpClient.execute(request);
driver.get("http://www.seleniumhq.org/download/");
driver.findElement(By.linkText("32 bit Windows IE")).click();
As per official release page of chrome driver, a fix has been introduced for this issue. Any chrome driver version greater than 77 will be able to download the file in headless mode.
options.addArguments("--headless");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.
Closed 6 months ago.
This post was edited and submitted for review 21 days ago.
Improve this question
I want to take all the network requests using selenium. I am not getting any way to find this solution if anyone can suggest to me or provide a code or library that would be appreciated.
This is my code:
ChromeOptions options = new ChromeOptions();
// Setting some chrome features here
ProxyServer proxyServer = new ProxyServer(4444);
proxyServer.start();
Proxy proxy = proxyServer.seleniumProxy();
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
capabilities.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new ChromeDriver(capabilities); // Error happens here
Not exactly open by dev tools but found some network, performance and other results.
Yes you can do that using JavascriptExecutor
Below Code will give you all performance, network etc entries:-
ChromeOptions options = new ChromeOptions();
options.addArguments("start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("http://www.google.com");
String scriptToExecute = "var performance = window.performance || window.mozPerformance || window.msPerformance || window.webkitPerformance || {}; var network = performance.getEntries() || {}; return network;";
String netData = ((JavascriptExecutor)driver).executeScript(scriptToExecute).toString();
System.out.println(netData);
OR Below Code will give you specific performance entries:-
DesiredCapabilities d = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.PERFORMANCE, Level.ALL);
d.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
WebDriver driver = new ChromeDriver(d);
driver.get("https://www.google.co.in/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
LogEntries les = driver.manage().logs().get(LogType.PERFORMANCE);
for (LogEntry le : les) {
System.out.println(le.getMessage());
}
The first code retrun network return network;" because of this JS tag. You can remove JS code of entity which you don't require
The second code return perfromance
Hope it will help you :)
It's working for me
ChromeOptions options = new ChromeOptions();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable( LogType.PERFORMANCE, Level.ALL );
options.setCapability( "goog:loggingPrefs", logPrefs );
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver(options);
driver.get("http://www.google.com");
List<LogEntry> entries = driver.manage().logs().get(LogType.PERFORMANCE).getAll();
System.out.println(entries.size() + " " + LogType.PERFORMANCE + " log entries found");
for (LogEntry entry : entries) {
System.out.println(entry.getMessage());
}
You can use "browsermob-proxy", "LoggingPreferences", "CloseableHttpClient", "HttpURLConnection" for getting the logs
If you wish not to use browser and want to get the response, then I would suggest to go for "CloseableHttpClient".
Copy the URI ("www.somewebsite.com/v1/api/sign-in?").
Get the request payload(which will be available in that particular API URI). Pass all the parameters with "&" like this "www.somewebsite.com/v1/api/sign-in?&username=xyz&password=1234566&app_id=12123214324234134&app_secret=213242345345345"
(Remember app id and app secret is very unique and do not expose it anywhere)
Once you get the URI, this code will give you JSON format response
HttpPost request = new HttpPost(str);
request.setHeader("content-type", "application/json");
HttpResponse response = client.execute(request);
BufferedReader bufReader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent()));
while ((line = bufReader.readLine()) != null) {
builder=String.valueOf(line);
}
System.out.println(builder);
}
My scenario is to close the chrome browser and open a new one.
public String openNewBrowserWindow() {
this.log("Opening new Browser window...");
String stringHandles;
Set<String> previousWindows = driver.getWindowHandles();
String previousHandle = driver.getWindowHandle();
((JavascriptExecutor)driver).executeScript("window.open();");
Set<String> newWindows = driver.getWindowHandles();
newWindows.removeAll(previousWindows);
String newHandle = ((String)newWindows.toArray()[0]);
stringHandles = previousHandle + ";" + newHandle;
return stringHandles;
}
What I did is this:
String handlesA = generic.openNewBrowserWindow();
String[] handleA = handlesA.split(";");
generic.closeBrowser();
generic.switchToWindow(handleA[1]);
This works on firefox but not in chrome. Do you guys have any suggestion?
Why not just:
driver.quit()
driver = new ChromeDriver()
What is your scenario really?
#Seimone
Whenever you want to intiate a Chrome browser, system property must be defined to the chromedriver.exe
System.setProperty("webdriver.chrome.driver", driverPath+"chromedriver.exe");
WebDriver driver = new ChromeDriver();
Also, If you want to close your current chrome browser window use the following one in your code.
driver.close();
If you want to close all your chrome browser window use the following one in your code.
driver.quit();
With reference to your scenario
Open the url
Login with signed in
Close the browser
Open the browser and enter the same url
Check the same user is logged in
Try the below code and let me know your result.
String chromeDriver = "enter the chromedriver.exe path";
String chromeProfile = "C:/Users/MSTEMP/AppData/Local/Google/Chrome/User Data/Default"; //Local chrome profile path.
System.setProperty("webdriver.chrome.driver", chromeDriver);
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments("test-type");
options.addArguments("start-maximized");
options.addArguments("user-data-dir="+chromeProfile);
capabilities.setCapability("chrome.binary",chromeDriver);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);
driver.get("https://www.gmail.com");
/*write your login credentials code with username, password and perform the
login operation with signed in*/
driver.close();
//Now invoke the chrome browser and enter the url alone.
driver = new ChromeDriver(capabilities);
driver.get("http://www.gmail.com");
//write the code for user signed verification.