URL is not opening in chrome in selenium - java

This is my code and requirement is that, need to open URL in same local chrome path. Is it possible??
public class Test{
public static void main(String args[]){
System.setProperty("webdriver.chrome.driver","C://Program Files (x86)//Google//Chrome//Application//chrome.exe");
WebDriver d= new ChromeDriver(); ;
String baseurl = "http://www.facebook.com/";
d.get(baseurl);
}
}

System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\Downloads\\chromedriver83\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
String baseurl = "http://www.facebook.com/";
driver.get(baseurl);

Check the path of chromedriver.exe carefully from your machine.
For Windows users:
System.setProperty("webdriver.chrome.driver","C:\\Users\\user\\Downloads\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");
For MAC users:
System.setProperty("webdriver.chrome.driver","//Users//apple//Downloads//chromedriver");
WebDriver driver = new ChromeDriver();
driver.get("http://www.facebook.com");

Related

org.openqa.selenium.WebDriverException: java.net.ConnectException , HOW CAN i RESOLVE

public class Prog1 {
public static void main(String[] args) {
System.setProperty("WebDriver.chrome.driver", "D:\\Users\\sh\\workspace\\Java2020\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver1.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
System.out.println("Hello world");
You need to replace this line; don't use upper case "Webdriver.chrome.driver"
System.setProperty("webdriver.chrome.driver", "C:/drivders/chromedriver.exe");
And also change this driver1 to use driver
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
Time out exception driver object should be driver instead of driver1

Java- Why Selenium Chrome webdiver is using my real IP adress instead of the proxy

I'm trying to browse with Selenium using a proxy but it fails and show my public IP instead
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("200.111.182.6:443");
option.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}
As per the Java Docs setCapability() method from MutableCapabilities is defined as:
setCapability
public void setCapability(java.lang.String key, java.lang.Object value)
So instead of setCapability(CapabilityType.PROXY, proxy) you have to use setCapability("proxy", proxy);. So effectively your code block will be:
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
Proxy proxy = new Proxy();
proxy.setHttpProxy("200.111.182.6:443");
option.setCapability("proxy", proxy);
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}
tl; dr
Capabilities & ChromeOptions
The following code solved my problem
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "D:\\driverChrome.exe");
ChromeOptions option = new ChromeOptions();
option.addArguments("--proxy-server=http://51.38.22.57:8522");
WebDriver driver = new ChromeDriver(option);
driver.get("https://whatismyipaddress.com");
driver.manage().window().maximize();
}

How to set Chrome Options when using WebDriverManager?

I'm using Web driver manager to setup chrome driver. When setting up the driver I want to add some chrome options? How can I do it when using web driver manager?
I checked the WebDriverManager API but couldn't find any clue..
As of WebDriverManager 5.x you can instantiate the webDriver directly via the WebDriverManager builder with additional capabilities this way (in java) :
WebDriver driver;
//...
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//...
//chromeOptions.addArguments(<another-option>);
//...
driver = WebDriverManager.chromedriver().capabilities(chromeOptions).create();
The capabilities method take a Capabilities as param.
Lucky we are, ChromeOptions implements the Capabilities interface.
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create Chrome Options
ChromeOptions option = new ChromeOptions();
option.addArguments("--test-type");
option.addArguments("--disable-popup-bloacking");
DesiredCapabilities chrome = DesiredCapabilities.chrome();
chrome.setJavascriptEnabled(true);
option.setCapability(ChromeOptions.CAPABILITY, option);
//Create driver object for Chrome
WebDriver driver = new ChromeDriver(option);
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
Found the answer.. Check above!
This is the example code:
public class Test1{
#Test
public void WebDriverManagerTest()
{
//setup the chromedriver using WebDriverManager
WebDriverManager.chromedriver().setup();
//Create driver object for Chrome
WebDriver driver = new ChromeDriver();
//Navigate to a URL
driver.get("http://toolsqa.com");
//quit the browser
driver.quit();
}
}
from https://pypi.org/project/webdriver-manager/, pass it in after .install()
from selenium import webdriver
from webdriver_manager.opera import OperaDriverManager
options = webdriver.ChromeOptions()
options.add_argument('allow-elevated-browser')
options.binary_location = "C:\\Users\\USERNAME\\FOLDERLOCATION\\Opera\\VERSION\\opera.exe"
driver = webdriver.Opera(executable_path=OperaDriverManager().install(), options=options)

Selenium: changing proxy in Firefox

i'm writing tests in selenium and want to change proxy to auto-detect in firefox, default is proxy from system settings. How to do it?
I have code below:
public class SodirRejestracja {
String baseUrl = "http://google.pl";
String driverPath= "C:\\geckodriver.exe";
WebDriver driver;
#BeforeTest
public void beforeTest() {
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 2);
System.setProperty("webdriver.gecko.driver", driverPath);
driver=new FirefoxDriver(profile);
}
#Test
public void test(){
driver.get("http://google.com");
}
}
Code above is from How do I set a proxy for firefox using Selenium webdriver with Java?
but in line driver=new FirefoxDriver(profile) i get: "The constructor FirefoxDriver(FirefoxProfile) is undefined"
A sample (not tested but that compiles) that should do it
String proxyName = <yourProxyHost> + ":" + <yourProxyPort>;
Proxy proxy = new Proxy();
proxy.setHttpProxy(proxyName)
.setFtpProxy(proxyName)
.setSslProxy(proxyName);
DesiredCapabilities desiredCapabilities = DesiredCapabilities.firefox();
desiredCapabilities.setCapability(CapabilityType.PROXY, proxy);
FirefoxOptions options = new FirefoxOptions(desiredCapabilities);
driver = new FirefoxDriver(options);
This code works
Proxy proxy = new Proxy();
proxy.setProxyType(Proxy.ProxyType.AUTODETECT);
FirefoxOptions options = new FirefoxOptions();
options.setProxy(proxy);
driver = new FirefoxDriver(options);

How to launch the Chrome Postman Plugin using Selenium Webdriver

ChromeDriver driver = new ChromeDriver();
ChromeOptions options=new ChromeOptions();
options.addArguments("chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm/index.html");
driver = new ChromeDriver(options);
Tried using the above code.
You can open extension page using below code :
public class sample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "yourpath/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("chrome://extensions/");
}
}
If you want to open plugin page then user below code :
public class sample {
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "yourpath/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://chrome.google.com/webstore/search/postman?hl=en-US");
}
}
Note : if you have not downloaded "chromedriver" yet , then please download from HERE

Categories

Resources