I have a Selenium WebDriver test that is failing after dismissing a modal window then waiting for an element. Specifically, it fails looking for an element in a FluentWait call. I've tried various different ways of setting up the wait, but none are working. Here is what I think should work:
The test code that is failing:
driver.findElement(By.className("dismiss")).click(); // this dismisses a modal window
driver.switchTo().defaultContent();
FluentWait<WebDriver> fluentWait = new FluentWait<WebDriver>(driver)
.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(NoSuchElementException.class);
fluentWait.until(ExpectedConditions.elementToBeClickable(driver.findElement(By.className("next"))));
driver.findElement(By.className("next")).click();
Here the failure happens looking for the element in the wait call:
org.openqa.selenium.NoSuchElementException: Unable to locate element:
{"method":"class name","selector":"next"} Command duration or timeout:
1.33 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info:
version: '2.41.0', revision: '3192d8a', time: '2014-03-27 17:17:32'
System info: host: '7rzrgv1.bsolver.local', ip: '10.0.3.103', os.name:
'Linux', os.arch: 'amd64', os.version: '2.6.32-431.el6.x86_64',
java.version: '1.7.0_60' Session ID:
56c5a752-68fd-4cdb-ae57-2fcefc930f6f Driver info:
org.openqa.selenium.firefox.FirefoxDriver Capabilities
[{platform=LINUX, acceptSslCerts=true, javascriptEnabled=true,
cssSelectorsEnabled=true, databaseEnabled=true, browserName=firefox,
handlesAlerts=true, browserConnectionEnabled=true,
webStorageEnabled=true, nativeEvents=false, rotatable=false,
locationContextEnabled=true, applicationCacheEnabled=true,
takesScreenshot=true, version=24.6.0}]
If I replace the fluentWait.until call with a Thread.sleep(5000), the click() works every time.
An interesting note... this only occurs when running the test on a headless machine running Firefox on top of Xvfb. It doesn't occur when an actual GUI is present.
So, a couple of questions:
Why is FluentWait not ignoring the NoSuchElementException ?
Does anyone have thoughts on a more elegant way to deal with this other than an implicit wait?
I had the same problem. #Karna and #Axl helped me figure it out. I was using import java.util.NoSuchElementException; instead of import org.openqa.selenium.NoSuchElementException;.
Related
I'm a newbie with Selenium WebDriver. I found some sample code, and adapted it a little bit to get my own test program. I'm having trouble with it! Here's the program:
package test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class SeleniumTester {
public static void main(String[] args) {
try {
System.setProperty("webdriver.chrome.driver", "C:\\Program Files\\Selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://www.oddsportal.com/");
driver.manage().window().maximize();
String str = driver.getCurrentUrl();
System.out.println("The current URL is " + str);
}
catch (Exception ex) {
System.out.println("Encountered exception in SeleniumTester.main() :");
System.out.println(ex.getMessage());
}
}
}
It compiles fine, and starts running, first outputting some diagnostics:
"Starting ChromeDriver (v2.9.248315) on port 11716
October 25, 2017 6:11:22 IP.
org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: OSS"
But then, at the line "driver.get(...);" an exception occurs :
"Encountered exception in SeleniumTester.main() :
unknown error: Runtime.executionContextCreated has invalid 'context': {"auxData":{"frameId":"8164.1","isDefault":true},"id":1,"name":"","origin":"://"}
(Session info: chrome=61.0.3163.100)
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.3 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
Build info: version: '3.6.0', revision: '6fbf3ec767', time: '2017-09-27T16:15:40.131Z'
System info: host: 'DESKTOP-47EFBCO', ip: '192.168.43.83', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_144'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome={userDataDir=C:\Users\Ilkka\AppData\Local\Temp\scoped_dir12200_17489}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=61.0.3163.100, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, platformName=XP, cssSelectorsEnabled=true}]
Session ID: 31a4a5806225e18533805ed0fbbca392"
Something about invalid context. What does it mean? How to fix it? Should I perhaps use a different constructor to create the ChromeDriver? Ideas, anyone?
Most likely the issue is because of an older version of chromedriver. Download a newer version of chromedriver and try giving its path.
The browser version and driver version compatibility issues could cause this issue. The chromedriver page mentions the driver compatibility for browser version. For example, as of now, the page lists:
Latest Release: ChromeDriver 2.33
Supports Chrome v60-62
I'm trying to locate a WebElement on google.com page using Appium and AndriodDriver. Selenium throws an Exception every time I'm trying to run the code bellow.
Client:
#Test
public void mobWebAppTest() throws Exception{
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName", "Appium");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("platformVersion", "4.3");
capabilities.setCapability("deviceName", "4df12fbe651cafd3");
capabilities.setCapability("appPackage", "com.android.browser");
capabilities.setCapability("appActivity", "BrowserActivity");
capabilities.setCapability(CapabilityType.BROWSER_NAME, "Android");
AndroidDriver<WebElement> driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[#name='q']")); // the error is thrown here
driver.quit();
}
Exception Logs:
org.openqa.selenium.NoSuchElementException: An element could not be located on
the page using the given search parameters. (WARNING: The server did not
provide any stacktrace information)
Command duration or timeout: 30.31 seconds
For documentation on this error, please visit:
http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.53.1', revision:
'a36b8b1cd5757287168e54b817830adce9b0158d', time: '2016-06-30 19:26:09'
System info: host: '*******', ip: '*******', os.name: 'Mac OS X', os.arch:
'x86_64', os.version: '10.10.5', java.version: '1.8.0_11'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{appPackage=com.android.browser, deviceScreenSize=720x1280,
networkConnectionEnabled=true, warnings={}, databaseEnabled=false,
deviceName=4df12fbe651cafd3, platform=LINUX, deviceUDID=4df12fbe651cafd3,
appActivity=BrowserActivity, desired={appPackage=com.android.browser,
appActivity=BrowserActivity, platformVersion=4.3, automationName=Appium,
browserName=Android, platformName=Android, deviceName=4df12fbe651cafd3},
platformVersion=4.3, webStorageEnabled=false, locationContextEnabled=false,
automationName=Appium, takesScreenshot=true, browserName=Android,
javascriptEnabled=true, deviceModel=GT-I9300, platformName=Android,
deviceManufacturer=samsung}]
Session ID: c68c1f7d-0e6e-481a-a335-23ede1de7de9
*** Element info: {Using=xpath, value=//input[#name='q']}
at ...
Also, when I'm trying to get a source code of the page, I'm getting back xml (as in ui automator) of the entire screen, not HTML of the google page. Could it be the reason for the exception?
THank you.
You need to switch to the web context first for DOM-level access:
driver.context("WEBVIEW_1");
You can get all available contexts with driver.getContextHandles().
To automate the "Browser" app with Appium, you need to use desired capability "browserName": "Browser" instead of setting the appPackage & appActivity.
https://appium.io/slate/en/master/?java#appium-server-capabilities
This is the button that I want to clicked. I want to check this button by writing automation scripts in selenium web driver by using Java.
The following is the code for the button.
<label class="toggle" style="font-size:13px">
<input type="checkbox" name="checkbox-IsEFR" id="checkbox-IsEFR">
<i data-swchon-text="Yes" data-swchoff-text="No"></i>Is Enable for Rules
</label>
Please help me since I am new to automation scripts writing. I wrote the script for check box but it is not working. The following, I have attached the my scripts and the error generated.
#When("^clicks on enable for rules$")
public void clicks_on_enable_for_rules() throws Throwable {
driver.findElement(By.name("checkbox-IsEFR")).click();
}
The error that I received.
org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Command duration
or timeout: 209 milliseconds Build info: version: '2.53.0',
revision: '35ae25b', time: '2016-03-15 17:00:58' System info: host:
'DELL_I5', ip: '192.168.1.33', os.name: 'Windows 10', os.arch:
'amd64', os.version: '10.0', java.version: '1.8.0_77' Driver info:
org.openqa.selenium.firefox.FirefoxDriver Capabilities
[{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true,
databaseEnabled=true, version=46.0, platform=WINDOWS,
nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true,
locationContextEnabled=true, browserName=firefox,
takesScreenshot=true, javascriptEnabled=true,
cssSelectorsEnabled=true}] Session ID:
03695ef8-bd24-49d6-b8a1-6687e3d0375c 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.ErrorHandler.createThrowable(ErrorHandler.java:206)
at
org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
at
org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:678)
at
org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:327)
at
org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:85)
at
mCollector.features.StepDefinitions_mCollector.clicks_on_enable_for_rules(StepDefinitions_mCollector.java:61)
at ✽.And clicks on enable for rules(C:/Users/Admin/workspace/MStudio
- eBilling/src/mCollector/features/mCollector.feature:12) Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not
currently visible and so may not be interacted with Build info:
version: '2.53.0', revision: '35ae25b', time: '2016-03-15 17:00:58'
System info: host: 'DELL_I5', ip: '192.168.1.33', os.name: 'Windows
10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_77'
Driver info: driver.version: unknown at .fxdriver.preconditions.visible(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:10092)
at .DelayedCommand.prototype.checkPreconditions_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12644)
at .DelayedCommand.prototype.executeInternal_/h(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12661)
at .DelayedCommand.prototype.executeInternal_(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12666)
at .DelayedCommand.prototype.execute/<(file:///C:/Users/Admin/AppData/Local/Temp/anonymous546390466745105063webdriver-profile/extensions/fxdriver#googlecode.com/components/command-processor.js:12608)
You can use explicit wait and ExpectedConditions to wait for the checkbox to be visible
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("checkbox-IsEFR"))).click();
This will wait up to 10 seconds for the element to be visible before clicking on it.
The Error is : "ElementNotVisibleException: Element is not currently visible and so may not be interacted with.."
There may be multiple reason of occurring this error but the strongest possibility is that:
The element is not loaded on the page and your code is trying to click it. To handle this you can put wait statement in your code. A global wait can be imposed by putting implicit wait driver.manage().timeouts().implicitlyWait(5000L, TimeUnit.SECONDS); or you can try putting explicit wait statement before performing the click activity.
Try the solution and I think it'll resolve your issue.
You can search by xpath in Selenium, using something like:
driver.find_element_by_xpath(".//*[contains(text(), 'Is Enable for Rules')]").click()
I am trying to execute some test cases using chrome driver but out of 4 test cases only 1 is getting executed and for rest of the three i am getting SessionNotFoundException. I searched for the similar question on the portal but all of them are related to Internet Explorer but i am running my test on chrome driver.
Selenium version- 2.42.2
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException: no such session
(Driver info: chromedriver=2.9.248315,platform=Windows NT 6.1 SP1 x86) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 128 milliseconds
Build info: version: '2.42.2', revision: '6a6995d', time: '2014-06-03 17:42:30'
System info: host: 'Admin-PC', ip: '10.0.0.6', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_31'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{applicationCacheEnabled=false, rotatable=false, chrome= {userDataDir=C:\Users\Admin\AppData\Local\Temp\scoped_dir4568_27180}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, version=41.0.2272.76, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
Session ID: 0fcb3e702dd1c8a23261b902625df216
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.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:599)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:614)
at org.openqa.selenium.remote.RemoteWebDriver.close(RemoteWebDriver.java:463)
at ResumeUpdate.Screen_1_Monster.operation(Screen_1_Monster.java:224)
at ResumeUpdate.Screen_1_Monster.main(Screen_1_Monster.java:341)
I changed some of the xpath and now the program won't throw any exception. I really not able to undertand that if the xpath was wrong then i should have recived NoSuchElementException instead of SessionNotFoundException. I tried to find an answer about why i am getting a wrong exception but till now not able to get a clear answer. Anyone who know about it please share the information
I also faced this problem recently and in my case browser was closing before test can run. e.g.
...........
"Login page" - {
"should have title" in {
pageTitle should be ("Login")
}
close()
}
..........
Replaced with
...........
"Login page" - {
"should have title" in {
pageTitle should be ("Login")
close()
}
}
..........
And fixed the issue.
If the Appium does not receive next command within 60 seconds it shuts down.
[BaseDriver] Shutting down because we waited 60 seconds for a command
[debug] [AndroidDriver] Shutting down Android driver
In my case, my automation was in a loop for more than 60 secs, for the app to finish it's task, which had network dependency.
You can avoid this by adding 'newCommandTimeout' to the capabilities, to increase timeout duration, like so:
capabilities.setCapability("newCommandTimeout", 90); //or seconds
Or you can add try / catch and log more meaningful message.
I get the following error on selenium,
I don't know how to fix it or what I should do,
unknown error: unhandled inspector error: {"code":-32603,"message":"Cannot navigate to invalid URL"}
(Session info: chrome=29.0.1547.57)
(Driver info: chromedriver=2.2,platform=Windows NT 6.1 SP1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 3 milliseconds
Build info: version: '2.35.0', revision: 'c916b9d', time: '2013-08-12 15:42:01'
System info: os.name: 'Windows Server 2008 R2', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_04'
Session ID: 6c0f76e6f67873a6318edab1d66e4ccf
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=XP, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={chromedriverVersion=2.2}, rotatable=false, locationContextEnabled=true, version=29.0.1547.57, cssSelectorsEnabled=true, databaseEnabled=true, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]
Command duration or timeout: 2.27 seconds
please help
I too got this error "org.openqa.selenium.WebDriverException: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot navigate to invalid URL"}"
To remove hardcoded URL and calling a URL from a file
I defined like this - ex: url="https://test.com" then I got the above error, after so many research and failures - simply I removed double quotes...it worked
ex: url=https://test.com
It happens if you´re using relative path to open the link. You can use absolute path (starting with http://) or - if it is an internal link/button on the web - you can mapp it as WebElement and perform click() method.
We managed to overcome the above error by changing the initialization Url. but now we are experiencing a bunch of new issues the latest chrome version, latest chromedriver and latest selenium versions.
Everything is extremely slow and we get many timeout since than.