Porting Selenium test from Firefox driver to HTMLUnit driver - java

I am trying to convert my selenium test from Firefox browser to HTMLUnit driver.
But, when I try to run the HTMLUnit test , it gives me error for XPATH.
The Firefox browser test runs absolutely fine.
My application test suite extensively uses XPATH .
Hence, I am intentionally trying out with XPATH.
I have already tried using
new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated
but still I got the same error.
This is the error :
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate a node using .//*[#id='tsf']/div[2]/div[3]/center/input[1]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: 'WL309476', ip: '10.83.16.25', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_66'
Driver info: driver.version: HtmlUnitDriver
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElementByXPath(HtmlUnitDriver.java:1161)
at org.openqa.selenium.By$ByXPath.findElement(By.java:361)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1715)
at org.openqa.selenium.htmlunit.HtmlUnitDriver$5.call(HtmlUnitDriver.java:1)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.implicitlyWaitFor(HtmlUnitDriver.java:1363)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:1711)
at org.openqa.selenium.htmlunit.HtmlUnitDriver.findElement(HtmlUnitDriver.java:606)
at seleniumtest.Test_Google.main(Test_Google.java:17)
This is my Firefox browser test :
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[#id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));
This is my HtmlUnit test :
WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
WebElement e =driver.findElement(By.xpath(".//*[#id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));
I don't think it is a duplicate as there is no javascript involved in my case.
I just want to port a simple test from Firefox driver to HTMLUnit.

Try to apply a little wait for the page to load and then try and locate your element. This is just to be sure that your code didn't execute while the page was still being loaded.
Try something like below,
WebDriver driver = new HtmlUnitDriver(true);
driver.get("https://www.google.co.in/");
Thread.sleep(5000);
WebElement e =driver.findElement(By.xpath(".//* [#id='tsf']/div[2]/div[3]/center/input[1]"));
System.out.println("The current element is " + e.getAttribute("value"));
The Thread.sleep(); will wait for the specified time before executing the next line.
Check with this and see if your element is found or not.
AND
Also check if your web page or the element is dependent on JavaScript. HTMLUnit Driver can't handle JavaScript and lacks of features which normal browsers have.

Related

Selenium WebDriver Setting debuggerAddress Stopped Working

I'm using Selenium Web driver, however, I am preloading a browser by setting the debuggerAddress. I connect through a specified port. I had this working great. Suddenly, it's stopped working. I'm not quite sure what the issue is.
public void launchBrowser() throws InterruptedException {
try {
// Set file path of chrome driver
System.setProperty("webdriver.chrome.driver", "C:\\chromedriver.exe");
// Start chrome driver with existing chrome browser
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("debuggerAddress","localhost:1250");
// Create object
driver = new ChromeDriver(options);
// Write to console where we are
System.out.println(driver.getCurrentUrl());
}
catch(Exception e) {
console("JacksClass.java","launchBrowser()", "Exception: " + e);
}
}
Before I run the script. I start the Chrome browser with this command in CMD
start chrome --remote-debugging-port=1250
After launching the browser from CMD successfully. Then running the code. I get the following exception after around 1-2 minutes.
JacksClass.java - launchBrowser() : Exception: 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.
Build info: version: '4.0.0-alpha-6', revision: '5f43a29cfc'
System info: host: 'DESKTOP-5H32IOI', ip: '192.168.1.53', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '11.0.8'
Driver info: driver.version: ChromeDriver
All of this was working great a month ago. I have the chrome driver installed in the same location.
Finally got it to work. Firstly I tried to access the open port in another browser.
Typing localhost:1250 into the URL bar.
It could not connect. So there clearly was an issue with that port. I have tried many different ports but still had the same issue.
I finally tried port 1258 and then double-checked it with the test outlined above. Typing localhost:1258 into another browser proved the port was active.
Then my program worked as well! How strange! Glad it's working now :)

Ask selenium to wait - WebDriverWait issue

I am facing an issue with selenium that I could not solve.
I am getting the following error"
no such element: Unable to locate element:{"method":"id","selector":"menu-supply-approval-queue"}
I know that that the issue is to wait.
So I made the next method:
public static WebElement getWebElementByIdWithWait(String id)
{
WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));
return WebDriverMgr.waitForElementToBeClickable(By.id(id));
}
However selenium not waits, and get this error again:
Thu Sep 12 16:56:45 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"id","selector":"menu-supply-approval-queue"}
(Session info: chrome=76.0.3809.132)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Can someone please advise how to make selenium to wait for the element to be displayed/clickable.
it is not waiting a second
what is more odd is that selenium actually returned the element and then clicks on it, so If he can not find it how he returned it? it just mess with the logs
Here is code that works, however it uses sleep
public static WebElement getWebElementByIdWithWait(String id)
{
Logger.sleep(60000);
// WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 8);
// wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));
return WebDriverMgr.waitForElementToBeClickable(By.id(id));
}
regards
This error message...
Thu Sep 12 16:56:45 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"id","selector":"menu-supply-approval-queue"}
(Session info: chrome=76.0.3809.132)
(Driver info: chromedriver=2.36.540470 (e522d04694c7ebea4ba8821272dbef4f9b818c91),platform=Windows NT 6.1.7601 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: '', ip: '', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
...implies that the ChromeDriver was unable to initiate/spawn a new WebBrowser i.e. Chrome Browser session.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
You are using chromedriver=2.36
Release Notes of chromedriver=2.36 clearly mentions the following :
Supports Chrome v63-65
You are using chrome=76.0
Release Notes of ChromeDriver v76.0 clearly mentions the following :
Supports Chrome version 76
Your JDK version is 1.8.0_65 which is pretty ancient.
So there is a clear mismatch between the JDK v8u65 , ChromeDriver v2.36 and the Chrome Browser v76.0
Solution
Ensure that:
JDK is upgraded to current levels JDK 8u222.
ChromeDriver is updated to current ChromeDriver v77.0 level.
Chrome is updated to current Chrome Version 77.0 level. (as per ChromeDriver v77.0 release notes)
Execute your #Test as non-root user.
There is an expected condition elementToBeClickable
WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 60);
wait.until(ExpectedConditions.elementToBeClickable(By.id("menu-supply-approval-queue")));
I'm not sure where/what the waitForElementToBeClickable call is doing and why it might be failing.
The method you posted has a String parameter of id but you hard coded a wait for a specific ID in the second line
public static WebElement getWebElementByIdWithWait(String id)
{
WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("menu-supply-approval-queue")));
^ this is hardcoded
return WebDriverMgr.waitForElementToBeClickable(By.id(id));
}
Have you tried removing that line?
public static WebElement getWebElementByIdWithWait(String id)
{
WebDriverWait wait = new WebDriverWait(WebDriverMgr.getDriver(), 300000000);
return wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(id));
}
If it were me, I would use a method like the below. It's not specific to IDs (you pass in a By so it can be used with IDs, CSS selectors, XPath, etc.) and it takes care of the click for you so the wait is specific to clickable.
public static void click(By locator)
{
new WebDriverWait(WebDriverMgr.getDriver(), 15).until(ExpectedConditions.elementToBeClickable(locator)).click();
}
You would call it like
click(By.id("menu-supply-approval-queue"));
Maybe you can try "Thread.Sleep(1000);" like
After investigation I found the issue, the Implicit wait was too small.
the soloution:

How to find an element within a frame in IE?

Code trials:
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'path')]")));
driver.findElement(By.id());
Switching to Frame is may be successful but it didn't throw any NoSuchFrameException.
The same is working in Chrome and FireFox. But the same is not Working in IE (tried in IE9 and IE11).
WebDriver Used: IEDriverServer Ver: 3.12.0.0
Is it because of the Document mode? or due to any Page rendering issue?
In What Case will this happen?
HTML Source Code Link: Source Code
Tried to Find the id MF:txtentdon
I don't know if this is important or not, but It also throws HTTP Status: '404' -> incorrect JSON status mapping for 'stale element reference' (400 expected)
Error Stack Trace:
org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #MF\:txtentdon
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.4.0', revision: 'unknown', time: 'unknown'
System info: host: 'N9776', ip: '172.29.18.139', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_131'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{proxy=Proxy(), acceptInsecureCerts=false, browserVersion=9, se:ieOptions={nativeEvents=true, browserAttachTimeout=0.0, ie.ensureCleanSession=false, elementScrollBehavior=0.0, enablePersistentHover=true, ie.browserCommandLineSwitches=, ie.forceCreateProcessApi=false, requireWindowFocus=false, initialBrowserUrl=http://localhost:6017/, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000.0, ignoreProtectedModeSettings=false}, timeouts={implicit=0.0, pageLoad=300000.0, script=30000.0}, browserName=internet explorer, pageLoadStrategy=normal, javascriptEnabled=true, platformName=windows, setWindowRect=true, platform=ANY}]
Session ID: 48da0488-5d2e-46db-996e-77f27f26ff28
*** Element info: {Using=id, value=MF:txtentdon}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:150)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:115)
at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:45)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:637)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:410)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:453)
at org.openqa.selenium.By$ById.findElement(By.java:218)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:402)
This error message...
org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #MF\:txtentdon
...implies that the InternetExplorerDriver server was unable to locate the desired element.
Your main issue seems to be the incompatibility between the version of the binaries you are using as follows:
You are using Selenium Java Client v3.4.0
You are using IEDriverServer v3.12.0.0
You are using JDK v1.8.0_131
So there is a clear mismatch between JDK v1.8.0-131, Selenium Client v3.4.0 and IEDriverServer v3.12.0.0.
Solution
While you work with Selenium Java Client, InternetExplorerDriver and Internet Explorer Browser ensure that:
You have fulfilled the Required Configuration as well as the additional considerations for Native Events and Browser Focus as per the documentation of InternetExplorerDriver.
You have upgraded JDK to recent levels JDK 8u171.
You have upgraded Selenium to current levels Version 3.12.0.
You have upgraded InternetExplorerDriver to current IEDriverServer v3.12.0 level.
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Use CCleaner tool to wipe off all the OS chores before and after the execution of your test Suite.
Take a System Reboot.
Execute your #Test.
Always invoke driver.quit() within tearDown(){} method to close & destroy the WebDriver and Web Client instances gracefully.
A couple of additional facts you need to consider while switching frame to locate element:
Always induce WebDriverwait while switching to desired frame.
Once you switch to the desired frame induce WebDriverwait while you look out for the desired element.
As your desired element is having the attribute readOnly="readonly" so you need to use the ExpectedConditions as visibilityOfElementLocated() as follows:
So your effective code block will be:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(#src,'path')]")));
WebElement element = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[#class='inputfld' and #id='MF:txtentdon']")));
You can try with this code :
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(#src,'path')]")));
System.err.println("inside frame");
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.id("MF:txtentdon")));
Or
WebElement element = new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.name("MF:txtentdon")));

Error SessionNotCreatedException with NativeConstructorAccessorImpl.newInstance0 while running selenium remotedriver

I am running the following but getting the error
public class base
{
public static WebDriver driver;
public static void main(String[] args) throws MalformedURLException, InterruptedException
{
System.setProperty("webdriver.chrome.driver", "C:\\code\\lib\\browser drivers\\chromedriver.exe");
String URL = "http://www.google.com";
String Node = "http://localhost:4444/wd/hub";
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap = DesiredCapabilities.chrome();
cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);
driver = new RemoteWebDriver(new URL(Node), cap);
driver.navigate().to(URL);
Thread.sleep(5000);
driver.quit();
}
}
Error shown is as follows :
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new service: ChromeDriverService
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: 'RAJESHW10', ip: '169.254.3.253', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_91'
Driver info: driver.version: unknown
Command duration or timeout: 316 milliseconds
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
Can somebody please help
The error does gives us a hint about what is going wrong as follows :
Exception in thread "main" org.openqa.selenium.SessionNotCreatedException: Unable to create new service: ChromeDriverService : Unable to create new service: ChromeDriverService which means the new session wasn't initiated.
Driver info: driver.version: unknown : driver.version: unknown means the chromedriver binary wasn't invoked at all.
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) : sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) which stems out from either of the following :
java.lang.RuntimeException: exception in constructor : Occurance of this exception is unique to reflection. Normally, it is impossible to write code which ignores a checked exception as it would not compile at all.
java.lang.reflect.InvocationTargetException : If an InvocationTargetException is thrown that means the method was invoked. This exception does not indicate a problem with the reflection package or its usage.
java.lang.IllegalArgumentException : An IllegalArgumentException is thrown if the constructor was passed an argument of the wrong type.
Conclusion
From the above mentioned ponints it is pretty clear that the version of Selenium-Java client, JDK, ChromeDriver binary and Chrome are not compatible.
Solution
The solution would be as follows :
Update your JDK with the recent releases (JDK 8u152)
Update your Selenium-Java client with the recent releases (Selenium-Java v3.8.1)
Update your ChromeDriver binary with the recent releases (ChromeDriver v2.34)
Update your ChromeDriver binary with the recent releases (Chrome v63.0)
Please do the following and retry.
Ensure that the path C:\\code\\lib\\browser drivers\\chromedriver.exe is added to your %PATH% variable on the machine in which you are running the node. This would ensure that the selenium uber jar can find the location of the chromedriver binary.
System.setProperty("webdriver.chrome.driver", "C:\code\lib\browser drivers\chromedriver.exe");
This mechanism should be only used when you are doing
ChromeDriver driver = ChromeDriver()
Since you are working with a selenium grid, you are dealing with multiple JVMs here.
System.setProperty() will affect only the current JVM (which in this case is your tests), but the actual browser gets spun off in a different JVM (the one that is running the selenium node).

Unable to create new Chrome remote session

I'm trying to launch a new Chrome browser using Selenium Grid but ending up with the below error
Unable to create new remote session. desired capabilities = Capabilities [{browserName=chrome, version=55.0.2, platform=WINDOWS}], required capabilities = Capabilities [{}]
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:49:13 -0700'
System info: host: 'PL9710388', ip: '10.61.249.5', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_111'
Driver info: driver.version: RemoteWebDriver
Below is my code to launch the Remote browser
java -Dwebdriver.chrome.driver=C:\chromedriver.exe -jar selenium-server-standalone-3.0.1.jar -role hub
java -Dwebdriver.chrome.driver=C:\chromedriver.exe -jar selenium-server-standalone-3.0.1.jar -role node
cap = DesiredCapabilities.chrome();
cap.setVersion("55.0.2");
cap.setBrowserName("chrome");
cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);
browser = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
Could you please help me on what is wrong?
make sure your code is able to find the chromedriver in your system. You can set the path programatically, you can even download and keep your driver from the below link
System.setProperty("webdriver.chrome.driver","/path to/chromedriver.exe");
cap = DesiredCapabilities.chrome();
cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);
browser = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
The line java -Dwebdriver.chrome.driver=C:\chromedriver.exe -jar selenium-server-standalone-3.0.1.jar -role node causes a plain vanilla node to be spun off which is agnostic of PLATFORM flavors (i.e., the node is not classified to recognize platform as a trait and is supposed to work as a generic node).
Your test code however seems to be specifying the platform as below
cap = DesiredCapabilities.chrome();
cap.setVersion("55.0.2");
cap.setBrowserName("chrome");
cap.setPlatform(org.openqa.selenium.Platform.WINDOWS);
To fix your problem please change your test code to look like below
cap = DesiredCapabilities.chrome(); // this sets the browser name. u dont need to do it again.
browser = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),cap);
Once you have this, you should be able to execute tests properly.
Please dont forget to add the location to where your chromedriver binary exists to your PATH variable before starting the node, so that you dont see issues related to selenium not being able to find the chromedriver's location.
For general overview on working with Grid, you can refer to my blog post
I encounter the same and i found that the platform, browser name & browser version details were not matching the grid configuration. Specifically it was because i was using platrom as windows where i would have used VISTA. Also make sure you are using the hub URL instead of the node URL.Hub URL would be http://hubIP:port/wd/hub.
Refer below screenshot to get the right details about the node:

Categories