I hope somebody could point me in the right direction. We're using Selenium Webdriver 2.28 with Internet Explorer version 8.
Our tests pass through the below method with the locator parameter is {By.XPath: //body}
private IWebElement WaitAndGetElement(By locator)
{
var wait = new WebDriverWait(_driver, TimeSpan.FromSeconds(Constants.DefaultWaitTimeSecs));
var elementToWaitFor = wait.Until(driver => driver.FindElement(locator));
return elementToWaitFor;
}
When the code hits that method. The exception below is thrown. This problem is quite reproducible with IE. We do not encounter this error with Chrome or FireFox.
If anyone could point us in the right direction, we would be very grateful.
Many thanks.
Christian Clarke
OpenQA.Selenium.InvalidSelectorException : The xpath expression '//body' cannot be evaluated or does notresult in a WebElement (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 156 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '2.28.0', revision: '18309', time: '2012-12-11 20:21:18'
System info: os.name: 'Windows 2003', os.arch: 'x86', os.version: '5.2', java.version: '1.6.0_31' Session ID: c72ebe1d-e7f0-4fdb-aab8-9f86b374a89a
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, javascriptEnabled=true, cssSelectorsEnabled=true, handlesAlerts=true, browserName=internet explorer, nativeEvents=true, takesScreenshot=true, version=8}]
Not my favorite approach, but try changing your XPath expression to ".//body", it's likely that it will work with this 'fix'.
Although i wouldn't advise using these locators on IE, since even on WebDriver they have proven to be slower, if possible, use CSS Locators.
How are you getting the XPATH? Is it with Firefox XPATH Checker addon? If so I would suggest just using the XPATH with Firebug. That one seems to work the best IMO with IE. In Firebug once you have the element selected you can right click the element and choose Copy XPATH.
Related
Selenium code to highlight an element is working only on one of the system.
I have updated chrome and chrome driver on both of them but in one machine, it works but code breaks on another machine when trying to highlight the page element.
Below is the exception:
An error occurred while fetching element : Expected condition failed: waiting for visibility of element located by By.id: body_x_grid_x__ctl2__ctl0 (tried for 15 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: '**', ip: '**', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, networkConnectionEnabled=false, chrome={chromedriverVersion=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab), userDataDir=**}, takesHeapSnapshot=true, pageLoadStrategy=normal, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=67.0.3396.99, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=false, acceptInsecureCerts=false, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true, setWindowRect=true, unexpectedAlertBehaviour=}]
Session ID: 9854688adcdbe56519b9869c496b58e2
at com.selenium.element.action.Wait.elementAction(Wait.java:68)
....
It does not find the element within specific periods and breaks.
This error message...
An error occurred while fetching element : Expected condition failed: waiting for visibility of element located by By.id: body_x_grid_x__ctl2__ctl0 (tried for 15 second(s) with 500 MILLISECONDS interval)
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: '**', ip: '**', os.name: 'Windows Server 2008 R2', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_77'
...implies that the ChromeDriver was unable to interact with the WebElement returned after WebDriverWait.
Your main issue is the incompatibility between the version of the binaries you are using as follows:
Your Selenium Client and ChromeDriver versions are not getting detected back.
Your JDK version is 1.8.0_77 which is pretty ancient.
So there is a clear mismatch between the JDK v8u77 and the latest Selenium Client v3.13.0 , ChromeDriver v2.40 and the Chrome Browser v67.0
Solution
Upgrade JDK to recent levels JDK 8u172.
Upgrade Selenium to current levels Version 3.13.0.
Keep ChromeDriver to current ChromeDriver v2.40 level.
Keep Chrome version between Chrome v66-68 levels. (as per ChromeDriver v2.40 release notes)
Clean your Project Workspace through your IDE and Rebuild your project with required dependencies only.
Execute your #Test.
Wrap your code in a try/catch clause, and inside the catch save the content of webdriver.getPageSource(). Then examine its content (you may even be able to open it in the browser if you name the file with a .html extension) and see whether an element with id='body_x_grid_x__ctl2__ctl0' actually exists.
I'm not sure, but it looks like this id was generated automatically by the application, and may be generated somewhat differently on the other machine.
Your problem is not with highlighter, it's with your webElement:
An error occurred while fetching element : Expected condition failed: waiting for visibility of element located by By.id: body_x_grid_x__ctl2__ctl0 (tried for 15 second(s) with 500 MILLISECONDS interval)
Increase the wait time because your existing wait in 15 sec: increase according to your website page load maximum time:
WebDriverWait wait = new WebDriverWait(driver, wait time in second);
WebElement we = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("your xpath")));
After getting the webElement you can go for highlighter by using below code:
public void highLighter(WebDriver driver, WebElement we) {
try{
JavascriptExecutor js = (JavascriptExecutor)driver;
String var = (String) js.executeScript("return arguments[0].getAttribute('style', arguments[1]);", we);
js.executeScript("return arguments[0].setAttribute('style', arguments[1]);", we,"border:4px solid red;");
Thread.sleep(200);
js.executeScript("return arguments[0].setAttribute('style', arguments[1]);", we,var);
}catch(Exception e){
System.out.println("unable to HighLight");
}
}
This might be a repeated question but I could not find any solution. Recently I found a related post Connecting Selenium WebDriver to an existing browser session but people suggested me to ask a new question.
If any one have tried connecting selenium webdriver to existing browser session that was earlier spawned by selenium itself and had success in doing so, please let me know.
I could find couple of suggestions to try on firefox and selenium 2.X version. But those suggestions do not work for selenium 3.X and there are no solutions for chrome browser.
I have tried all suggestions for Selenium 25.3, firefox v 46 and it works. But for Chrome with chrome driver , I am not able to make it work.
Edited:
Here is the code I have tried:
Starting a firefox driver
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir")+"/StartFirefoxSession_lib/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
Copied RemoteWebDriver source code and changed capabilities from private to protected.
protected Capabilities capabilities;
Created a new class RemoteDriverEx extending the copied RemoteWebDriver class
Changed the NEW_SESSION command issued by the original driver to GET_CURRENT_URL
Response response = execute(DriverCommand.GET_CURRENT_URL, Collections.EMPTY_MAP);
Then craeted a JUnit test to verify
But I am struck with exception
org.openqa.selenium.WebDriverException: No command or response codec has been defined. Unable to proceed
Build info: version: 'unknown', revision: 'unknown', time: 'unknown'
System info: host: 'WPANDBW7HYD', ip: '192.168.56.1', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_74'
Driver info: driver.version: RemoteWebDriver
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:154)
Full code shared # https://drive.google.com/open?id=0Bz2XxuQQc24KdHVqR3BPaXowUnM
It is possible in selenium all you need is a debugger address of the session you want to connect to. If you are wondering what is debugger address its nothing but the localhost address on which your session is running, it looks like localhost:60003. Now it will be different for each and every case. Below is process with c# code.
Get debugger Address of browser you want to connect later using debug mode as shown in snapshot below. debug driver after browser launch to fetch value
Now keep that browser running and to reconnect the same browser use below code.
ChromeOptions option = new ChromeOptions();
option.DebuggerAddress="localhost:60422";// we need to add this chrome option to connect the required session
driver = new ChromeDriver(option);
driver.Navigate().GoToUrl("https://www.google.com/");
Hope this helps!! let me know in comments if any clarification is required.
I managed to find a solution for Firefox in a hack way that works local:
First, you need to start a separate instance of browser (manual start) using the following arguments:
firefox.exe --marionette -profile C:\FirefoxTEMP
Above we open an instance of Firefox with --marionette turned on and we choose a fixed profile folder that were created just for selenium tasks.
Now, we will attach our automation to the already open Firefox window, by adding an argument to chose the same profile we started before.
Note: You must chose the same profile folder for the Webdriver use the open instance.
FirefoxOptions options = new FirefoxOptions();
options.AddArguments("--profile C:\\FirefoxTEMP");
driver = new FirefoxDriver(options);
driver.Navigate().GoToUrl("https://google.com");
Safari 10.0.1
macOS Sierra
When running Codeception command:
$I->waitForElementVisible(['css' => 'input[type=text][id=UserUsername]'], 30);
in an acceptance test in Safari with Selenium 3.0.1 I receive an error. The screenshot taken at failure clearly displays the element in question. The same test/command is successful in both Firefox and Chrome. The error:
Screenshot saved to /Applications/MAMP/htdocs/AutomatedTests/tests/_output/debug/FAILED1479307207.png
Unable to retrieve Selenium logs : The command 'GET /session/9BC56414-8934-4315-9293-B6E99720E318/log/types' is not implemented.
Command duration or timeout: 3 milliseconds
Build info: version: '3.0.1', revision: '1969d75', time: '2016-10-18 09:48:19 -0700'
System info: host: 'Cosettes-MacBook-Pro.local', ip: '10.0.1.75', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.12.1', java.version: '1.8.0_101'
Driver info: org.openqa.selenium.safari.SafariDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, databaseEnabled=true, handlesAlerts=true, version=12602.2.14.0.5, cleanSession=true, platform=MAC, nativeEvents=true, locationContextEnabled=false, webStorageEnabled=true, browserName=safari, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 9BC56414-8934-4315-9293-B6E99720E318
Screenshot and page source were saved into '/Applications/MAMP/htdocs/AutomatedTests/tests/_output/' dir
ERROR
When I run the same test/command in Safari/Firefox/Chrome with Selenium 2.53.1 it finds the element with no problems.
Is there a known issue with this type of locator I'm not finding when going through the forums? Anyone have a suggestion for how to make this work?
Update 12-01-16: This now seems to be more of an issue with waitForElementVisible() command than the Locator. If I change the command to $I->waitForElement(['css' => 'input[type=text][id=UserUsername]'], 30); the test successfully moves forward till the next waitForElementVisible() command.
People say visibility checks are broken in the release version of Safari 10. You can try Safari Technology Preview, and if your issue is still there, we can conclude it's some other issue, not the broken visibility checks. If your issue is gone, it'll be not exactly your users' experience, but better than nothing anyways. Also you can try implementing your own visibility checks as a workaround using some script on the browser's side (e.g. this function looks good enough).
To run your tests in Safari Technology Preview, add
'safari.options': { technologyPreview: true }
to the capabilities.
See also my other answer on this subject.
Elements are not get in IE Browser. i am using IE 11 Browser. While I am run my code then Error is displaying in my consol as Started InternetExplorerDriver server (32-bit) 2.47.0.0
Listening on port 20577
Error show := org.openqa.selenium.NoSuchElementException: Unable to find element with id == Id_user_name_id (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 10.17 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.47.1', revision: '411b314', time: '2015-07-30 02:56:46'
System info: host: 'VDJSDEV2-PC', ip: '172.16.1.220', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_60'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, enablePersistentHover=true, ie.forceCreateProcessApi=false, pageLoadStrategy=normal, ie.usePerProcessProxy=false, ignoreZoomSetting=false, handlesAlerts=true, version=11, platform=WINDOWS, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:20577/, takesScreenshot=true, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, cssSelectorsEnabled=true, unexpectedAlertBehaviour=dismiss}]
Session ID: 8bd16d35-c13d-46f7-b6cf-f9a7ddd54dda
*** Element info: {Using=id, value=Id_user_name_id}
And I am using these Jar files
selenium-server-standalone-2.47.1.jar
selenium-ie-driver-2.47.0.jar
selenium-java-2.47.1-srcs.jar
selenium-java-2.47.1.jar
IEDriverServer_Win32_2.47.0
I found the same problem, and my solution is:
Modified IE setting to display both secure and non secure contents.
Set the Protected Mode settings for each zone to be the same value, the value can be on or off, as long as it is the same for every zone (I checked "Enable Protected Mode" all).
Way to set protected mode: choose "Internet Options..." from the Tools menu -> click on the Security tab. For each zone (Internet/Local Internet/Trusted Web Sites/Restricted Sites), check the check box at the bottom of the tab labeled "Enable Protected Mode".
Restart your IE browser.
It works on me, hope it can help you too.
Some suggestion which may help.
Check the element id is unique in the page and actually exist; can you post the html page ( just the element and its parent parent if possible)
Increase the browser capabilities timeout to double the existing one
before you search for the element by id
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
Try with different Driver (Firefox or Chrome) and see if the problem persist
Try to search the element by selector; if you post the html page, I can help in sending some suggestion
Cheers
Alan Mehio
London
I am using selenium 2.47 with Firefox v31. With this simple implementation:
public void navigateToHomePage() throws Throwable {
System.out.println("Navigate to Home");
driver = new FirefoxDriver();
driver.quit();
}
I got this error:
org.openqa.selenium.WebDriverException: Unable to bind to locking port 7054 within 45000 ms
Build info: version: '2.47.0', revision: '0e4837e', time: '2015-07-29 22:49:49'
System info: host: 'ok-ThinkPad-SL500', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '3.13.0-24-generic', java.version: '1.7.0_79'
Driver info: driver.version: FirefoxDriver
at org.openqa.selenium.internal.SocketLock.lock(SocketLock.java:99)
at org.openqa.selenium.firefox.internal.NewProfileExtensionConnection.start(NewProfileExtensionConnection.java:90)
at org.openqa.selenium.firefox.FirefoxDriver.startClient(FirefoxDriver.java:276)
at org.openqa.selenium.remote.RemoteWebDriver.<init>(RemoteWebDriver.java:116)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:223)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:216)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:212)
at org.openqa.selenium.firefox.FirefoxDriver.<init>(FirefoxDriver.java:125)
at cucumber.features.StepDefinitions.navigateToHomePage(StepDefinitions.java:24)
at ✽.Given I navigate to the home site(/home/ok/workspace/CucumberPOC/src/cucumber/features/UserRegistry.feature:6)
I don't know what is it??
This SocketLock error happens when Firefox tries to bind to port 7054 and fails because another instance already has that port locked.
An immediate solution is make sure you have NO Firefox tasks or process running in the background before launching a new driver. Kill them all and try launching again.
In the long run, you can avoid these issues by creating a new profile, change the port preferences to avoid locks, and then launch the driver using that profile. A short example:
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference(FirefoxProfile.PORT_PREFERENCE, 7046)
driver = new FirefoxDriver(profile);
Unfortunately, when I was looking up the Java terms, I found that this feature was broken in Java's driver once, then it was fixed and then it broke in a later update, so even if you implement the solution I gave you, you may still get these errors in 2.47.
The fix was released in version 2.43.1, so if you don't need the newer versions for another issue/feature, you can try rolling back to a point where you can set a port preference successfully.
Not required reading: Some more technical details of the issue are in this pull request.