I am new in selenium. I need a browser without a graphical interface because the project will start with Jenkins. I decided to use ChromeDriver in Headdless mode.
When I use ChrimeDriver in normal mode, I can click on all elements:
WebDriver driver = new ChromeDriver();
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click(); // is ok
}
But when I use Headdless mode then I have: ElementNotVisibleException: element not visible. What could be wrong? Thank you for every clue.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
//chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
List<WebElement> allElem = driver.findElements(By.ByXPath("//div[#id='accordian']/div/ul/li"));
for(int i=0; i<allElem.getSize(); i++){
allElem.get(i).click();//ElementNotVisibleException dont see next li elements
//div[#id='accordian']/div/ul/li
}
While working with Selenium Client v3.11.0, Chrome Driver v2.36 and Chrome Browser v65.x in Headless Mode, you need to pass the following arguments through an instance of ChromeOptions Class while initializing the WebDriver and the Web Browser as follows :
System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("start-maximized");
chromeOptions.addArguments("--disable-gpu");
chromeOptions.addArguments("--disable-extensions");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://www.google.co.in");
You need to pass "--headless", chrome option like below.
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
WebDriver driver = new ChromeDriver(chromeOptions);
For entire list of chrome options, refer the following URL. It explains every command line switches in detail.
https://peter.sh/experiments/chromium-command-line-switches/
While working with headless mode, I encountered org.openqa.selenium.UnhandledAlertException due to not handling popping out of Alert Boxes. So it is better if you could handle the alert boxes.
String alertText = alert.getText();
System.out.println("ERROR: (ALERT BOX DETECTED) - ALERT MSG : " + alertText);
alert.accept();
File outputFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
String imageDetails = "D://Images"
File screenShot = new File(imageDetails).getAbsoluteFile();
FileUtils.copyFile(outputFile, screenShot);
System.out.println("Screenshot saved: {}" + imageDetails);
driver.close();
Related
Here is my current code to launch browser without any proxy:
properties = getGridProperties();
DesiredCapabilities capabilities = null;
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("layout.css.devPixelsPerPx","0.9");
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
options.addPreference("dom.webnotifications.enabled", false);
if (properties.containsKey("hub.gecko.driver.path"))
System.setProperty("webdriver.gecko.driver", properties.getProperty("hub.gecko.driver.path"));
capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, profile);
if (browserType.equalsIgnoreCase("oldfirefox")) {
capabilities.setCapability("marionette", false);
capabilities.setCapability("platform", "LINUX");
options.merge(capabilities);
}
printInfo("initRemoteWebDriver:started:" + System.currentTimeMillis());
capabilities.setCapability("idleTimeout", 150);
String nodeURL = "http://" + properties.getProperty("grid.hub.host") + "/wd/hub";
capabilities.setCapability("idleTimeout", 150);
capabilities.setCapability("name", this.getClass().getCanonicalName());
driver = new RemoteWebDriver(new URL(nodeURL), capabilities);
setDriver(driver);
getDriver().manage().window().maximize();
printInfo("****Firefox browser launched***");
printInfo("initRemoteWebDriver:finished:" + System.currentTimeMillis());
Desired proxy details to be set:
HTTP_PROXY = 'http://www-proxy.us.abc.com:80'
HTTPS_PROXY = 'http://www-proxy.us.abc.com:80'
What is the simplest way to do this without changing the current code too much?
Hi please try this one
Proxy proxy = new Proxy();
proxy.setHttpProxy("http://www-proxy.us.abc.com:80");
capabilities.setCapability(CapabilityType.PROXY, proxy);
I hope it will work for you.
Thanks
Please refer Selenium documentation - https://www.selenium.dev/documentation/en/webdriver/http_proxies/
An alternative would be to use JVM flags. That way you don't have to change your code at all.
java -Dhttp.proxyHost=http://www-proxy.us.abc.com -Dhttp.proxyPort=80 -Dhttp.nonProxyHosts=”localhost|127.0.0.1|10.*.*.*”
I'm a bit new to selenium and I was looking to use it to run various automated tasks through Tor. I'm on Windows 10 using Java through Eclipse with the Selenium packages as referenced libraries. I've checked out several other tutorials and forum threads detailing how to set up Tor in selenium. I started out trying to use the following code:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
WebDriver driver = new FirefoxDriver(binary, torProfile);
driver.get("https://www.google.com");
Using that code produces this error:
Exception in thread "main" org.openqa.selenium.WebDriverException: Cannot find firefox binary in PATH. Make sure firefox is installed.
I tried referencing this thread for a solution. I then used the following code based off of that thread:
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.10.0-win64\\geckodriver.exe");
String torPath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath =
"C:\\Users\\Dave\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile torProfile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
torProfile.setPreference("webdriver.load.strategy", "unstable");
try
{
binary.startProfile(torProfile, new File(profilePath), "");
}
catch (IOException e)
{
e.printStackTrace();
}
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
WebDriver driver = new FirefoxDriver(binary, profile);
driver.get("https://www.google.com");
Using that code launches Tor for me; however, I still receive the same error as before and the driver cannot control that Tor window.
I would appreciate any help or advice.
Im currently trying to execute TOR 6.0.4 with Selenium WebDriver (JAVA) 2.53 and Firefox 43.0. I've followed the instructions from this post Using Selenium WebDriver with Tor but Im getting an error while loading the profilePath of TOR to the Firefox Binary. I've seen that is possible to lunch TOR by loading the TOR profile.default archive to the firefox binaty, but Im getting a Driver info: driver.version: unknown, when instantiating the binary with the profile. I've tried to change the firefox version and still. Below the code where I start the driver. Im also using Windows.
String torPath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Start Tor Browser.exe";
String profilePath = "C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxDriver driver = new FirefoxDriver();
driver.get("http://www.google.com/webhp?complete=1&hl=en");
See below the exception thrown:
Exception in thread "main" org.openqa.selenium.WebDriverException: Specified firefox binary location does not exist or is not a real file: C:\Users\Jose Bernhardt\Desktop\Tor Browser\Start Tor Browser.exe
Seems that I was loading the Tor.exe and instead I had to load the firefox.exe from the Tor archive. I change my path to this and is working. Also fix that I was not sending the profile and the binary to the driver constructor
"C:\\Users\\Jose Bernhardt\\Desktop\\Tor Browser\\Browser\\firefox.exe"
FirefoxDriver driver = new FirefoxDriver(binary, torProfile);
System.setProperty("webdriver.firefox.marionette", ".\\geckodriver.exe");
String torPath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "C:\\Users\\HP\\Desktop\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
File torProfileDir = new File(profilePath);
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
FirefoxOptions options = new FirefoxOptions();
options.setBinary(binary);
options.setProfile(torProfile);
options.setCapability(FirefoxOptions.FIREFOX_OPTIONS,options);
WebDriver driver = new FirefoxDriver(options);
Till now Tor browser is supported using the Mozilla Firefox
Here is the code below:
System.setProperty("webdriver.gecko.driver", "C:\\Users\\user\\eclipse-workspace\\TorSelenium\\src\\main\\resources\\geckodriver.exe");//sa used for the firefox
String torBinaryPath = "C:\\Users\\user\\OneDrive\\Desktop\\Lk's stuff\\Tor Browser\\Browser\\firefox.exe"; //It is inside the tor browser's folder
Runtime runTime = Runtime.getRuntime();
Process torProcess = runTime.exec(torBinaryPath + " -n");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
WebDriver driver;
driver = new FirefoxDriver(firefoxOptions);
driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
driver.manage().window().maximize();
WebDriverWait wait;
wait = new WebDriverWait(driver, 30);
JavascriptExecutor js = (JavascriptExecutor) driver;
//added end
System.out.println(ean);
//Thread.sleep(100);
//driver.navigate().to("https://www.google.com/?hl=en");
//driver.navigate().to("https://duckduckgo.com/?q=d&ia=web");
driver.navigate().to("https://www.swiggy.com");
If you want to change the tor identity then you have to restart the tor using and again start above code:
Runtime.getRuntime().exec("taskkill /f /IM firefox");
Runtime.getRuntime().exec("taskkill /f /IM firefox.exe");
if(torProcess.isAlive()) {
System.out.println("destroying tor");
torProcess.destroy();
}
if(torProcess.isAlive()) {
System.out.println("forcebly destroying tor");
torProcess.destroyForcibly();
}
I need a Webdriver instance of Tor to be able to launch google.com in tor from selenium, java code in Windows and be able to search for a Target String
My code is below
String torPath = "..Installations\\Tor\\Tor Browser\\Browser\\firefox.exe";
String profilePath = "..Installations\\Tor\\TorBrowser\\Browser\\TorBrowser\\Data\\Browser\\profile.default";
FirefoxProfile profile = new FirefoxProfile(new File(profilePath));
FirefoxBinary binary = new FirefoxBinary(new File(torPath));
FirefoxDriver driver = new FirefoxDriver(binary, profile);
driver.get("http://www.google.com");
This results in a blank Tor Browser page opening. It does not load google.com as desired. I know that the profile is valid/compatible because I can successfully start the browser and profile with:
binary.startProfile(profile, profilePath, ""));
I have looked at similar questions but did not get satisfactory answer.
Can it be done ? If yes , how ?
I am looking for Java code.
FirefoxDriver firefoxDriver;
File torProfileDir = new File("C:\\tor\\Tor Browser\\Browser\\TorBrowser\\Data\\Browser\\profile.default");
FirefoxBinary binary = new FirefoxBinary(new File( "C:\\tor\\Tor Browser\\Browser\\firefox.exe"));//C:\tor\Tor Browser\Browser
FirefoxProfile torProfile = new FirefoxProfile(torProfileDir);
torProfile.setPreference("webdriver.load.strategy", "unstable");
binary.startProfile(torProfile, torProfileDir);
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.type", 1);
profile.setPreference("network.proxy.socks", "127.0.0.1");
profile.setPreference("network.proxy.socks_port", 9150);
use this code
I download ChromeDriver and by defaults the browser language is in English, I need to change it to Spanish, and I have been unable.
public WebDriver getDriver(String locale){
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
return new ChromeDriver();
}
public void initializeSelenium() throws Exception{
driver = getDriver("en-us")
}
You can do it by adding Chrome's command line switches "--lang".
Basically, all you need is starting ChromeDriver with an ChromeOption argument --lang=es, see API for details.
The following is a working example of C# code for how to start Chrome in Spanish using Selenium.
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=es");
ChromeDriver driver = new ChromeDriver(options);
Java code should be pretty much the same (untested). Remember, locale here is in the form language[-country] where language is the 2 letter code from ISO-639.
public WebDriver getDriver(String locale){
System.setProperty("webdriver.chrome.driver", "driver/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("--lang=" + locale);
return new ChromeDriver(options);
}
public void initializeSelenium() throws Exception{
driver = getDriver("es"); // two letters to represent the locale, or two letters + country
}
For me, --lang didn't work. It seems to set the language of the first opened tab, all others chrome processes are started with --lang=en-US.
What did work is the following:
DesiredCapabilities jsCapabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
Map<String, Object> prefs = new HashMap<>();
prefs.put("intl.accept_languages", language);
options.setExperimentalOption("prefs", prefs);
jsCapabilities.setCapability(ChromeOptions.CAPABILITY, options);
I had problems with Chrome using US date format (mm/dd/yyyy) instead of the GB dd/mm/yyyy format (even though I had set these in Chrome). Using:
options.addArguments("--lang=en-GB");
resolved this.
As of now (Jan 2020 - Chrome Version 79.0.3945.130) The C# in the accepted answer does not work.
The simplest approach that I can find to work in C# presently:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("intl.accept_languages", language);
WebDriver driver = new ChromeDriver(chromeOptions);
I was trying the same and above listed nothing worked for me, at last I tried the below and it worked:
ChromeOptions chromeOptions = new ChromeOptions();
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("intl.accept_languages", "ja-jp,ja");
chromeOptions.setExperimentalOption("prefs", prefs);
WebDriver driver = new ChromeDriver(chromeOptions);
For me --lang also didn't work. I wanted to perform Facebook Login tests with specific language (en-US instead of en-GB) and what I found is that some pages (like Facebook) set interface according to system environment variable LANG... So if above answers doesn't work, try changing LANG environment variable. Tested on Linux.
I had the same problem. I tried to solve the problem including
chromeOptions = Options()
chromeOptions.add_argument('--lang=es')
but it didn't work (I have discovered that isn't necessary).
It works when I changed the locale:
locale -a
sudo apt-get install language-pack-es
sudo dpkg-reconfigure locales
It is es_ES.UTF-8 UTF-8 for Spanish. Finally, you have to start a new shell in order to get new env variables (LANG=C.UTF-8 to es_ES.UTF-8)
For latest versions below code should work.
ChromeOptions options = new ChromeOptions();
options.addArguments("--accept-lang=" + locale);
return new ChromeDriver(options);
https://peter.sh/experiments/chromium-command-line-switches/#accept-lang
For people using Selenium with ruby:
I made it work this way:
prefs_hash = {
'credentials_enable_service' => false,
'profile' => {
'password_manager_enabled' => false,
},
'intl.accept_languages' => 'fr-FR', // <- here
}
// [...]
browser = Watir::Browser.new :chrome, :prefs => prefs_hash, switches: switches, desired_capabilities: caps
website = 'https://e-katalog.lkpp.go.id/id/search-produk?q=' + mylist[i] +
'&order=relevance&limit=12&offset=' + str(1)
chrome_options = Options()
chrome_options.add_argument("--lang=en");
chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])
prefs = {
"translate_whitelists": {'id':'en'},
"translate":{"enabled":"True"}}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Chrome('C:\webdrivers\chromedriver.exe',options=chrome_options)
time.sleep(10)
driver.get(website)
time.sleep(5)