C# Selenium, can't find any elements after logging in - c#

I use Selenium with ChromeDriver. When I open up a driver and manually go to a webpage, and then start running my code, everything works fine. However, if I log in to the website and manually go to the same page as before (it's the exact same page but logged in), I won't even find the html element:
var el = driver.FindElement(By.CssSelector("html"));
returns
OpenQA.Selenium.WebDriverException: The HTTP request to the remote WebDriver server for URL http://localhost:........../elements timed out after 60 seconds.
The assignment
var el = driver.FindElements(By.CssSelector("*"));
returns the same thing, there's literally nothing. Shouldn't I at least get a "cannot find element" exception?
Now if I log out it works again (everything is done in the same instance of ChromeDriver)

Related

Getting Error while using SendKeys with PhantomJs C#

I am trying to use phantomjs for one of our application to perform headless browser testing. I am stuck with below error, when trying to enter a value into the field:
OpenQA.Selenium.WebDriverException : Unexpected error. TypeError -
undefined is not a constructor (evaluating
'_getTagName(currWindow).toLowerCase()')
It works perfectly with Chrome, FF, IE and also with headless Chrome. I did some research, but not able to resolve the issue.
Understand that PhantomJs is faster than any other browser so added explicit wait and tried with Thread.Sleep, but still the same issue. However, click on button on the same page works properly, finding issue with SendKeys.
Below is the code ( phantomjs 2.2.1 ):
var driver = new PhantomJSDriver();
driver.Navigate().GoToUrl("https://portal-sandbox.afterpay.com");
driver.Manage().Window.Maximize();
driver.Manage().Cookies.DeleteAllCookies();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.ClassName("button")));
Thread.Sleep(1000);
//taking screenshot to debug
sc = driver.GetScreenshot();
sc.SaveAsFile("f://atest1.jpg");
var email = driver.FindElement(By.CssSelector("input[name=\"email\"]"));
email.SendKeys("abc#xyz.com"); //getting error on this line
driver.FindElementByClassName("button").Click();
sc = driver.GetScreenshot();
sc.SaveAsFile("f://atest2.jpg");
Tried following different solutions
1) google.com with entering values it works fine (just to check if sendKeys works)
2) updated PhantomJs.exe 1.9.8, but the same result.
3) Javascript executor to enter the value - no luck
4) Used Actions to build and perform - same result
So, not sure, if this issue is with phantomJs or with application not supporting entering values with phantomJs.
Any Help really appreciated.

HTTP request to the remote WebDriver server for URL timeout when running tests across browsers in parallel

I've been battling the commonly seen "HTTP request to the remote WebDriver server for URL ... timed out after x seconds" for several months in an attempt to run tests against two browsers simultaneously (Chrome and IE), spending hours at a time trawling through stackoverflow and search engine results to attempt to find a solution.
My behavior, like others before me, varies between timing out at a click function or when attempting to get a url, and I have increased pageload and implicit wait timeouts to over 600 seconds in various cases, inserting the waits before an element, after a url is called, before a url is called, after the driver constructor is called and as a parameter in the driver object call.
I have attempted to include javascript exectutor scripts (provided from answers in previous SO posts on this issue) that check for the page load ready state to be complete before continuing with an action, to no success.
I have attempted to update my chrome and IE, selenium web and support drivers all to the latest compatible versions, manually calling the binary for the latest compatible browser executable - as well as attempting to roll back to previous versions where people have reported success (chrome v48, chromedriver 2.22.0.0, webdriver 2.53.1). I've tried adding "no-sandbox" as a chrome option, ensured that my IE security zones all shared the same level of protection.
I've investigated whether my page is using AJAX scripts and attempted to use the solutions provided in various threads to accommodate for any dynamic content.
When running either IE or Chrome individually, outside of the parallel query, no timeout issues are observed. The issue specifically occurs when chrome initializes its remote WebDriver instance. I've also tried using 32bit and 64bit versions of the chrome/ie drivers.
I've pulled information out of many topics, and pages, but these are some of the most relevant ones.
Selenium Error - The HTTP request to the remote WebDriver timed out after 60 seconds
https://sqa.stackexchange.com/questions/13326/the-http-request-to-the-remote-webdriver-server-timed-out-after-60-seconds
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5441
https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/5071
Selenium Error - The HTTP request to the remote WebDriver timed out after 60 seconds
Selenium WebDriver throws Timeout exceptions sporadically
Here's an example of the output:
System.Reflection.TargetInvocationException : Exception has been thrown by the target of an invocation.
----> System.AggregateException : One or more errors occurred.
----> OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL http://localhost:52240/session/a969dbe2-3b0c-461f-a979-21bafec0dd8e/element/7005aeab-ff31-454a-8f78-0a39ad861695/click timed out after 120 seconds.
----> System.Net.WebException : The request was aborted: The operation has timed out.
I call the drivers from a case list where there are later added in to the parallel query:
private static IWebDriver DefineDriver(Browser supportedbrowsers)
{
var baseDriverPath = ConfigurationManager.AppSettings["BaseDriverPath"].ToString();
var ieDriverFolder = ConfigurationManager.AppSettings["IeDriverFolder"].ToString();
var chromeDriverFolder = ConfigurationManager.AppSettings["ChromeDriverFolder"].ToString();
ChromeOptions chromeoptions = new ChromeOptions();
chromeoptions.BinaryLocation = #"C:\WebDrivers\Binary\chrome32_49.0.2623.75\chrome.exe";
chromeoptions.AddArgument("no-sandbox");
InternetExplorerOptions ieoptions = new InternetExplorerOptions();
ieoptions.IntroduceInstabilityByIgnoringProtectedModeSettings = false;
IWebDriver driver = null;
switch (supportedbrowsers)
{
case Browser.Chrome:
driver = new ChromeDriver(Path.Combine(baseDriverPath, chromeDriverFolder), chromeoptions, TimeSpan.FromMinutes(5));
break;
case Browser.InternetExplorer:
driver = new InternetExplorerDriver(Path.Combine(baseDriverPath, ieDriverFolder), ieoptions, TimeSpan.FromMinutes(5));
break;
default:
driver = new ChromeDriver(Path.Combine(baseDriverPath, chromeDriverFolder), chromeoptions, TimeSpan.FromMinutes(5));
break;
}
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(120));
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromMinutes(10));
driver.Manage().Timeouts().SetScriptTimeout(TimeSpan.FromMinutes(10));
driver.Manage().Window.Maximize();
return driver;
}
In my test code, I am simply launching a page, navigating to another local page and then attempting to click a button that is immediately visible on the page.
I've tried wrapping the click command for the button in a try catch, added explicit waits with expected conditions (displayed, enabled, isclickable), used thread sleep which have all worked as expected when running a single browser.
For example, I call the button via:
public void SelectAddWorkWorkPageButton()
{
WebDriverWait wait = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.ElementToBeClickable(addNewWorkItemWorkPageBtn));
addNewWorkItemWorkPageBtn.Click();
}
Which locates the following element:
//Create New Button
[FindsBy(How = How.Id, Using = "btnWorkDefinitionCreateNewWorkDefinition")]
public IWebElement addNewWorkItemWorkPageBtn { get; set; }
And it's HTML:
<i id="btnWorkDefinitionCreateNewWorkDefinition" title="Add work" class="fa fa-plus-circle cursorPointer crudIcon" style="font-size: 20px;margin:0;padding-left:15px" ng-click="AddNewWorkDefinition()" role="button" tabindex="0"></i>
As a separate note regarding the timeouts, when updating to the latest versions of the WebDriver, I have also updated the timeouts to their new format:
//driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(120);
//driver.Manage().Timeouts().PageLoad = TimeSpan.FromSeconds(120);
//driver.Manage().Timeouts().AsynchronousJavaScript = TimeSpan.FromSeconds(120);
This issue seems to have existed in the community since as far back as 2012 and has, to my finding, never been isolated and clearly identified, with people still reporting it in May this year.
Selenium Error - The HTTP request to the remote WebDriver timed out after 60 seconds
After attempting several more workarounds, including the use of Protractor to account for AngularJS code, I've finally isolated the cause of the "The HTTP request to the remote WebDriver server for URL" exception message down to a specific issue when running the IEDriver individually or in parallel against the application in test.
It appears that the application was using a SignalR connection to handle certain processes and this causing the IEDriver actions (such as a click event) to timeout because the SignalR connection would never complete and therefore result in the IEDriver not being able to determine that the page had finished loading before it could perform another action.
When the SignalR connection type was updated to use "Long Polling" this resolved the IEDriver timeout issue entirely.
Better explanations are available in these posts, and much credit is owed to those who contributed within, I'd never have guessed that SignalR was the cause otherwise:
https://github.com/SignalR/SignalR/issues/293
SignalR w/ Web Sockets
C# Protractor AngularJS IEDriverServer Click() Exception "Timed out waiting for page to load"
Thanks.

Selenium - Getting "timed out after 60 seconds" error when using Exists method

I've been trying possible solutions that I can find online with the bottom error message with no luck.
An exception of type 'OpenQA.Selenium.WebDriverException' occurred in WebDriver.dll but was not handled in user code
Additional information: The HTTP request to the remote WebDriver server for URL http://localhost:49357/session/8490ba8026d4a3d1bb60f08d2063ae79/elements timed out after 60 seconds.
protected bool Exists(By by)
{
return wd.FindElements(#by).Any();
}
When I run any of my scripts that come across the "Exists" method (posted above) where I'm expecting the element it's searching for to NOT BE there, I get the error message "The HTTP request...timed out after 60 seconds.". However, this only happens when I run the test in Chrome and the element doesn't exist on the page (when it does exist, it has no issues with the Exists method). When I run the script with Firefox, it has no issues at all.
I've tried extending my commandTimeout to twice the time, but it still fails at 60 seconds. Tried changing the "webDriver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(###))" to twice it's wait length, no success. Looked through everything in my code base that has a timeout method and tried to change those, no success there either. I even went as far as change the ChromeOptions BinaryLocation to be pointed at the actual chromedriver.exe file.
If anyone can suggest on how to find the default location of the correct wait command or how to solve my issue with how to identify when an element isn't present on the screen, that would help immensely.

Can't load html from local file in PhantomJS

Using Selenium and PhantomJS, I can load a file from a server running on localhost like this:
_driver.Url = "http://localhost:52873/Home/Template";
Where _driver is an instance of RemoteWebDriver that connects to an instance of PhantomJS running on a port (launched with --webdriver=9134). And I can call a Javascript function defined in that HTML file like so:
var val = _driver.ExecuteScript("myFunction()");
However, I would like to load it from the local file system rather than through the local webserver, so I tried something like this:
_driver.Url = "file://c:/path/to/Home/template.html";
An I get no errors, but when I try to execute a script that is defined in the html file, it does not work. It throws an InvalidOperationException with the message Can't find variable: myFunction. However, if I set _driver to an instance of ChromeDriver instead, I can see the page load and everything works as expected, both using http and file protocols.
I get no additional information from the PhantomJS console window, so I don't really know what the problem is. I suspect for some reason PhantomJS just isn't loading the page. Any ideas?
Also, is there any way using the .NET binding for selenium to get access to the console log? I added a console.log("connected") to my html file to run as soon as the page loads, but it doesn't get echoed to the phantomJS console either when run from file or from http, so that's not much help.
Looks like you are missing an extra / in the path URI.
_driver.Url = "file:///c:/path/to/Home/template.html";

Selenium Error - The HTTP request to the remote WebDriver timed out after 60 seconds

I've been using Selenium for a number of months, which we're using to automate some of our internal testing processes. The scripts have been passing fine. I've recently upgraded to C# 2.40.0 webdriver using FF 27.01 and our scripts are now failing in random places with the following error.
[Portal.SmokeTest.SmokeRunTest.Booking] TearDown method failed. OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/element timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/window timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
[09:01:20]
[Portal.SmokeTest.SmokeRunTest.Booking] TearDown method failed. OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/element timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
TearDown : OpenQA.Selenium.WebDriverException : The HTTP request to the remote WebDriver server for URL htt(p)://localhost:7055/hub/session/56e99e88-ba17-4d12-bef1-c6a6367ccc2f/window timed out after 60 seconds.
----> System.Net.WebException : The operation has timed out
at OpenQA.Selenium.Support.UI.DefaultWait`1.PropagateExceptionIfNotIgnored(Exception e)
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
at Portal.Test.Helpers.Process_Bookings.OpenBookings.SelectBooking(String bookingnumber)
at Portal.SmokeTest.SmokeRunTest.Booking() in d:\TeamCityAgent\work\dac1dcea7f2e80df\SmokeTests\SmokeRunTest.cs:line 68
--WebException
at System.Net.HttpWebRequest.GetResponse()
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
--TearDown
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Firefox.Internal.ExtensionConnection.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.Close()
at Portal.Test.Helpers.Setup.CloseWebdriver()
at Portal.SmokeTest.SmokeRunTest.TearDown() in d:\TeamCityAgent\work\dac1dcea7f2e80df\SmokeTests\SmokeRunTest.cs:line 162
--WebException
at System.Net.HttpWebRequest.GetResponse()
at OpenQA.Selenium.Remote.HttpCommandExecutor.CreateResponse(WebRequest request)
The latest error I've managed to track down to one single line of code:
_setup.driver.FindElement(By.XPath("//button[#class='buttonSmall lockBookingButton']")).Click();
The annoying thing is, trying to fix the problem is proving difficult, as if I run the test on my local machine, in debug it passes. Additionally, if I run it via the NUNIT runner on the build machine I'm running the test off, it also passes. It only seems to fail as part of our automated build running process when using Teamcity. Like I said, this has been running fine for months previously, and the only thing that has changed is the selenium webdriver kit.
I have experienced this problem before, whilst in debug, and when a Click() line of code was called, Firefox appeared to lock up, and only stopping the test would allow Firefox to continue. There are a number of suggestions on here including modifying the webdriver source? I'd like to not go down that route if possible if anyone else can offer any suggestions.
I had a similar issue using the Chrome driver (v2.23) / running the tests thru TeamCity. I was able to fix the issue by adding the "no-sandbox" flag to the Chrome options:
var options = new ChromeOptions();
options.AddArgument("no-sandbox");
I'm not sure if there is a similar option for the FF driver. From what I understand the issue has something to do with TeamCity running Selenium under the SYSTEM account.
new FirefoxDriver(new FirefoxBinary(),new FirefoxProfile(),TimeSpan.FromSeconds(180));
Launch your browser using the above lines of code. It worked for me.
I first encountered this issue months ago (also on the click() command), and it has been an issue for me ever since. It seems to be some sort of problem with the .NET Selenium bindings. This blog post by the guy that works on the IE driver is helpful in explaining what's happening:
http://jimevansmusic.blogspot.com/2012/11/net-bindings-whaddaymean-no-response.html
Unfortunately, there doesn't seem to be a real solution to this problem. Whenever this issue has been raised to the Selenium developers (see here), this is a typical response:
We need a reproducible scenario, that must include a sample page or a link to a public site's page where the issue can be reproduced.
If you are able to submit a consistently reproducible test case, that could be very helpful in putting this bug to rest for good.
That said, perhaps you can try this workaround in the meantime. If the HTML button that you are trying to click() has an onclick attribute which contains Javascript, consider using a JavascriptExecutor to execute that code directly, rather than calling the click() command. I found that executing the onclick Javascript directly allows some of my tests to pass.
Had same issue with Firefox. I switched over to Chrome with options and all has been fine since.
ChromeOptions options = new ChromeOptions();
options.AddArgument("no-sandbox");
ChromeDriver driver = new ChromeDriver(ChromeDriverService.CreateDefaultService(), options, TimeSpan.FromMinutes(3));
driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));
In my case, my button's type is submit not button and I change the Click to Sumbit then every work good. Something like below,
from driver.FindElement(By.Id("btnLogin")).Click();
to driver.FindElement(By.Id("btnLogin")).Submit();
BTW, I have been tried all the answer in this post but not work for me.
Got similar issue. Try to set more time in driver's constructor - add eg.
var timespan = TimeSpan.FromMinutes(3);
var driver = new FirefoxDriver(binary, profile, timeSpan);
In my case, it's because I deleted the chrome update folder. After chrome reinstall, it's working fine.
In my case none of the answers above solved my problem completely.
I ended up using the (no-sandbox) mode, the connection with extended timeout period (driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability, TimeSpan.FromMinutes(3));) and the page load timeout (driver.Manage().Timeouts().PageLoad.Add(System.TimeSpan.FromSeconds(30));) so now my code looks like this:
public IWebDriver GetRemoteChromeDriver(string downloadPath)
{
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArguments(
"start-maximized",
"enable-automation",
"--headless",
"--no-sandbox", //this is the relevant other arguments came from solving other issues
"--disable-infobars",
"--disable-dev-shm-usage",
"--disable-browser-side-navigation",
"--disable-gpu",
"--ignore-certificate-errors");
capability = chromeOptions.ToCapabilities();
SetRemoteWebDriver();
SetImplicitlyWait();
Thread.Sleep(TimeSpan.FromSeconds(2));
return driver;
}
private void SetImplicitlyWait()
{
driver.Manage().Timeouts().PageLoad.Add(TimeSpan.FromSeconds(30));
}
private void SetRemoteWebDriver()
{
driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability, TimeSpan.FromMinutes(3));
}
But as I mentioned none of the above method solved my problem, I was continuously get the error, and multiple chromedriver.exe and chrome.exe processses were active (~10 of the chromedriver and ~50 of chrome).
So somewhere I read that after disposing the driver I should wait a few seconds before starting the next test, so I added the following line to dispose method:
driver?.Quit();
driver?.Dispose();
Thread.Sleep(3000);
With this sleep modification I have no longer get the timeout error and there is no unnecessarily opened chromedriver.exe and chrome.exe processses.
I hope I helped someone who struggles with this issue for that long as I did.
I think this problem occurs when you try to access your web driver object after
1) a window has closed and you haven't yet switched to the parent
2) you switched to a window that wasn't quite ready and has been updated since you switched
waiting for the windowhandles.count to be what you're expecting doesn't take into account the page content nor does document.ready. I'm still searching for a solution to this problem
In my case I found this error happening in our teams build server. The tests worked on our local dev machines.
The problem was that the target website was not configured correctly on the build server, so it couldn't open the browser correctly.
We were using the chrome driver but I'm not sure that makes a difference.
The problem is that the evaluation of Click() times out on your build env.. you might want to dig into what happens on Click().
Also, try adding Retrys for the Click() because occssionally the evaluations take longer time depending on network speeds, etc
In my case the issue was with SendKeys() and Remote Desktop. Posting the workaround I have so far:
I had a Selenium test which would fail when run as part of a Jenkins job on a node hosted in vSphere and administered through RDP. After some troubleshooting it turned out it succeeds if Remote Desktop is connected and focused but fails with the exception if Remote Desktop is disconnected or even minimized.
As a workaround, I logged through vSphere Console instead of RDP and then even after closing vSphere the test didn't fail anymore. This is a workaround but I would have to be careful never to login through RDP and always to administer only through vSphere Console.
We had the same problem. In our case, the browser was blocked by a login popup (Windows authentication), so not returning after 60 seconds. Adding correct access rights to the Windows account Chrome was running under solved the problem.
I had the same exception when trying to run a headless ChromeDriver with a scheduled task on a windows server (unattended). What solved it for me is to run the task as the user "Administrators" (notice the S at the end). What I also did (I don't know if its relevant) is selected the "Any Connection" from the task "Conditions" tab.
I was getting this issue. I'd neglected to update the Selenium.WebDriver.ChromeDriver nuget package to match the version of Chrome I was using. Once I did that the issue was resolved.
changing the Selenium.WebDriver.ChromeDriver from 2.40.0 to 2.27.0 is ok for me
The new FirefoxDriver(binary, profile, timeSpan) has been obsolete.
You can now use new FirefoxDriver(FirefoxDriverService.CreateDefaultService(), FirefoxOptions options, TimeSpan commandTimeout) instead.
There is also a new FirefoxDriver(string geckoDriverDirectory, FirefoxOptions options, TimeSpan commandTimeout) and it does works. But it's undocumented, and you need to manually specify geckoDriverDirectory even though it's already in Path.
Arrrgh! Faced this on macOS today and the issue was as simple as - the pop-up window suggesting to install the new Appium version was showing on the remote CI build server.
Just VNC'ing to it and clicking "Install later" fixed it.
I see this issue as well when running my tests so have been searching for the 'why' and the 'how to fix'.
My initial thought (after looking at the screenshot failures and the timeout message) was that the website performance must have gotten worse and that the website is taking longer to do some stuff which is causing the issue.
I've found a lot of solutions (which I plan to try out) HOWEVER, it seems like Most of the solution sort of indicate that the website performance (reaction time) got slower. If you have to change wait for 30 seconds to wait for 3 minutes, it is true that this will allow the test to pass ... but doesn't this mean we are having to wait around 3 minutes for the website to do something?
Question: Can 90% of the time this error message be chalked up to degradation of the website performance? I read some sites mentioned above and it sounds like the issue could also be with the chromedriver causing delays (maybe nothing to do with application issues). Is anyone brining this issue up to their application development team or are you just changing the wait times and not reporting?
I posted in selenium slack channel on how the timeout that we pass for Chromedriver constructor works and got this answer(C#).
"The timeout that you can set in the driver constructor is the 'command timeout' which is essentially the read timeout for the http client. The http client knows nothing about what the driver is doing.
If the page never loads and the command timeout is less than the page load timeout, you'll get an error saying "HTTP request to the remote WebDriver server timed out".
If the command timeout is greater than the page load timeout, the driver will respond to the client with a timeout error, and Selenium will report the message received by the driver."
For ChromeDriver the below worked for me:
string chromeDriverDirectory = "C:\\temp\\2.37";
var options = new ChromeOptions();
options.AddArgument("-no-sandbox");
driver = new ChromeDriver(chromeDriverDirectory, options,
TimeSpan.FromMinutes(2));
Selenium version 3.11, ChromeDriver 2.37

Categories