package main_files;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class main_downloader {
public static void main(String[] args) throws Exception {
System.setProperty("webdriver.gecko.driver", "path\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.wait(1);
driver.get("https://www.google.com/");
driver.wait(5);
driver.quit();
}
}
for some reason when I try to run this code it gives me this Error
1597445198205 geckodriver INFO Listening on 127.0.0.1:7834
1597445198836 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "--marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\ahmed\\AppData\\Local\\Temp\\rust_mozprofileinTqzS"
JavaScript error: resource://gre/modules/XULStore.jsm, line 66: Error: Can't find profile directory.
console.error: SearchCache: "_readCacheFile: Error reading cache file:" (new Error("", "(unknown module)"))
1597445201342 Marionette INFO Listening on port 22663
1597445201445 Marionette WARN TLS certificate errors will be ignored for this session
Aug 15, 2020 1:46:41 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Exception in thread "main" java.lang.IllegalMonitorStateException
at java.lang.Object.wait(Native Method)
at main_files.main_downloader.main(main_downloader.java:10)
i've tried some solutions but nothing worked !, could anyone help please !? Thx.
wait() function call must be called from Synchronized block to avoid this exception.
Do you want to sleep the main thread , if yes please use Thread.sleep() instead of wait() function call.
Related
I always get the following warnings when i ty to execute my code...
The code works but it is annoying and i would like to fix it!
1580642544984 mozrunner::runner INFO Running command: "/usr/lib/firefox/firefox" "-marionette" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofilevW0se4"
1580642546026 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid host permission: resource://pdf.js/
1580642546027 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid host permission: about:reader*
(firefox:7348): Gtk-WARNING **: 12:22:26.596: Theme parsing error: <data>:1:34: Expected ')' in color definition
(firefox:7348): Gtk-WARNING **: 12:22:26.596: Theme parsing error: <data>:1:77: Expected ')' in color definition
1580642549203 Marionette INFO Listening on port 43209
1580642549310 Marionette WARN TLS certificate errors will be ignored for this session
Feb. 02, 2020 12:22:29 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
And here the code:
package com.Selenium_intelliJ;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Main {
public static void search(WebDriver obj, String search){
obj.findElement(By.name("q")).sendKeys(search);
obj.findElement(By.xpath("/html/body/div/div[3]/form/div[2]/div[1]/div[3]/center/input[1]")).submit();
}
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/home/josef/Java/geckodriver/geckodriver");
WebDriver obj = new FirefoxDriver();
obj.get("https://www.google.com/xhtml");
search(obj, "test");
}
}
Thanks in advance!
I want selenium to force the browser to reload the page which it is loading if the loading process takes too long.
From StackOverflow I have that this code
new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
.executeScript("return document.readyState").equals("complete"));
will wait until the page is fully loaded, but I want it to be reloaded if it takes more than 30 seconds.
How can I achieve that?
To reload the webpage incase the loading process is taking too long you can configure pageLoadTimeout. pageLoadTimeout sets the amount of time to wait for a page load to complete before throwing an error. If the timeout is negative, page loads can be indefinite.
An example (using Selenium v3.141.59 and GeckoDriver v0.24.0):
Code Block:
public class pageLoadTimeout
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().pageLoadTimeout(2, TimeUnit.SECONDS);
try{
driver.get("https://www.booking.com/hotel/in/the-taj-mahal-palace-tower.html?label=gen173nr-1FCAEoggJCAlhYSDNiBW5vcmVmaGyIAQGYATG4AQbIAQzYAQHoAQH4AQKSAgF5qAID;sid=338ad58d8e83c71e6aa78c67a2996616;dest_id=-2092174;dest_type=city;dist=0;group_adults=2;hip_dst=1;hpos=1;room1=A%2CA;sb_price_type=total;srfid=ccd41231d2f37b82d695970f081412152a59586aX1;srpvid=c71751e539ea01ce;type=total;ucfs=1&#hotelTmpl");
// do your other work here
}catch(WebDriverException e){
System.out.println("WebDriverException occured");
}
driver.quit();
}
}
Console Output:
1565680787633 mozrunner::runner INFO Running command: "C:\\Program Files\\Mozilla Firefox\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\Debanjan.B\\AppData\\Local\\Temp\\rust_mozprofile.3jw3aiyfNAiQ"
1565680826515 Marionette INFO Listening on port 56499
1565680827329 Marionette WARN TLS certificate errors will be ignored for this session
Aug 13, 2019 12:50:28 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Aug 13, 2019 12:50:31 PM org.openqa.selenium.remote.ErrorCodes toStatus
WebDriverException occured
You can find a relevant discussion in pageLoadTimeout in Selenium not working
You can find a detailed discussion in Do we have any generic function to check if page has completely loaded in Selenium
WebDriverWait with through timeout exception. Put your code inside try/catch and reload the page on timeout exception:
try {
new WebDriverWait(driver, 30).until((ExpectedCondition<Boolean>) wd -> ((JavascriptExecutor) wd)
.executeScript("return document.readyState").equals("complete"));
} catch (TimeoutException e) {
// log a timeout
// System.out.println("Page load timeout, refresh.");
driver.navigate().refresh();
}
Try _driver.Navigate().Refresh();
I just started a selenium project, but things didn't got right, so after searching a bit I found this solution. It works but i can't understand what these red statements want me to do, or how to get rid of them.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.*;
public class SelTest1
{
public static void main(String [] args)
{
System.setProperty("webdriver.gecko.driver","X:\\Gecko\\geckodriver-v0.24.0-win64\\geckodriver.exe");
File pathBinary = new File("X:\\FireFoxx\\firefox.exe");
FirefoxBinary firefoxBinary = new FirefoxBinary(pathBinary);
DesiredCapabilities desired = DesiredCapabilities.firefox();
FirefoxOptions options = new FirefoxOptions();
desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxBinary));
WebDriver obj = new FirefoxDriver(options);
obj.get("http://www.google.com/");
}
}
I got the result i wanted, but I don't understand the warning{ red statements}
I am putting those red lines { warnings here too for ease }
Jul 12, 2019 7:07:28 PM org.openqa.selenium.remote.DesiredCapabilities firefox
INFO: Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`
1562938650997 mozrunner::runner INFO Running command: "X:\\FireFoxx\\firefox.exe" "-marionette" "-foreground" "-no-remote" "-profile" "C:\\Users\\adars\\AppData\\Local\\Temp\\rust_mozprofile.uTUmeENutxin"
1562938652637 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: mozillaAddons
1562938652638 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: resource://pdf.js/
1562938652638 addons.webextension.screenshots#mozilla.org WARN Loading extension 'screenshots#mozilla.org': Reading manifest: Invalid extension permission: about:reader*
1562938655909 Marionette INFO Listening on port 50040
1562938655964 Marionette WARN TLS certificate errors will be ignored for this session
Jul 12, 2019 7:07:36 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
This INFO log message:
INFO: Using `new FirefoxOptions()` is preferred to `DesiredCapabilities.firefox()`
is the result of the changes incorporated in:
Selenium v3.0.0-beta4
Added ability to use FirefoxOptions when starting firefox.
Selenium v3.5.0
* Start making *Option classes instances of Capabilities. This allows
the user to do:
`WebDriver driver = new RemoteWebDriver(new InternetExplorerOptions());`
If your usecase is to explicitly mention the absolute location of the FirefoxBinary you can use the following solution:
Using FirefoxOptions:
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
public class A_Firefox_binary
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "C:/Utility/BrowserDrivers/geckodriver.exe");
FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
WebDriver driver = new FirefoxDriver(options);
driver.get("https://stackoverflow.com");
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();
}
}
Console Output:
Page Title is : Stack Overflow - Where Developers Learn, Share, & Build Careers
I get a warn message when I try this code but the test case execute successfully.
Code
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class teatcalss {
public static void main(String[] args) {
// declaration and instantiation of objects/variables
System.setProperty("webdriver.gecko.driver", "D:\\java\\geckodriver-v0.14.0-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
String baseUrl = "http://newtours.demoaut.com";
String expectedTitle = "Welcome: Mercury Tours";
String actualTitle = "";
// launch Firefox and direct it to the Base URL
driver.get(baseUrl);
// get the actual value of the title
actualTitle = driver.getTitle();
/*
* compare the actual title of the page witht the expected one and print
* the result as "Passed" or "Failed"
*/
if (actualTitle.contentEquals(expectedTitle)){
System.out.println("Test Passed!");
} else {
System.out.println("Test Failed");
}
//close Firefox
driver.close();
// exit the program explicitly
System.exit(0);
}
}
Warning message
1486664295999 geckodriver INFO Listening on 127.0.0.1:44072
Feb 09, 2017 10:18:16 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
log4j:WARN No appenders could be found for logger (org.apache.http.client.protocol.RequestAddCookies).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
1486664297761 mozprofile::profile INFO Using profile path C:\Users\Radwa\AppData\Local\Temp\rust_mozprofile.DhhwmmiiHibT
1486664297779 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe
1486664297822 geckodriver::marionette INFO Connecting to Marionette on localhost:10262
[GFX1-]: CreateShaderResourceView failed for format87
1486664301804 Marionette INFO Listening on port 10262
[GFX1-]: CreateShaderResourceView failed for format87
Feb 09, 2017 10:18:29 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
Test Passed!
This is a known message. See here for more info and discussion.
Bottom line, it's not an error, it won't break anything. If something gets screwed up, then that's when it's a good time to investigate. As it is right now, you can safely ignore it.
You can use the below code with your scripts, which will redirects these low level logs to a separate file rather than the console -
System.setProperty(FirefoxDriver.SystemProperty.DRIVER_USE_MARIONETTE,"true");
System.setProperty(FirefoxDriver.SystemProperty.BROWSER_LOGFILE,"C:\\temp\\logs.txt");
For more information, you check refer this article - Disable Firefox logs
I am getting the following problems with Selenium Test Run. I am using the tests to create and run on macSierra (10.12) Processor 2.8 GHz Intel Core i7.
I have installed Eclipse, Selenium and Java files from Selenium site.
Here is my code :
package com.some.seleniumproject;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.*;
import org.openqa.selenium.WebDriver.*;
import org.openqa.selenium.firefox.*;
public class sampleTest1 {
public static void main (String[] args) {
System.setProperty("webdriver.gecko.driver", "//Library/Java/geckodriver");
WebDriver driver=new FirefoxDriver();
String baseUrl = "http://www.seleniumhq.org";
String et = "Selenium - Web Browser Automation";
driver.get(baseUrl);
String rt = driver.getTitle();
if (rt.contentEquals(et)) {
System.out.println("Title is matched");
}
else {
System.out.println("Title is not matched");
}
driver.close();
}
}
Here are the errors :
1 :
1477536446864 geckodriver INFO Listening on 127.0.0.1:11419
Oct 26, 2016 7:47:27 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Attempting bi-dialect session, assuming Postel's Law holds true on the remote end
1477536448247 mozprofile::profile INFO Using profile path /var/folders/dk/xyw6v2zd1bd5sp8pd_4f8fpw0000gn/T/rust_mozprofile.jDvUQye5JfoO
1477536448249 geckodriver::marionette INFO Starting browser /Applications/Firefox.app/Contents/MacOS/firefox-bin
1477536448307 geckodriver::marionette INFO Connecting to Marionette on localhost:56571
[warn] kq_init: detected broken kqueue; not using.: Undefined error: 0
1477536450843 Marionette INFO Listening on port 56571
1477536453622 Marionette INFO startBrowser dab7ebb7-1664-6943-9cc4-e3817dd3a894
Oct 26, 2016 7:47:34 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
[warn] kq_init: detected broken kqueue; not using.: Undefined error: 0
However, the above error maybe, correct result is thrown in the output.
2 :
Note: This element has neither has attached source nor attached Java doc and hence no Java
doc could be found. - this is for the step driver.close();
Please help.
I've found this bug reported in Mozilla - https://bugzilla.mozilla.org/show_bug.cgi?id=1304266. Read a comment to it.
It looks like libevent issue can cause the problem you've described.