C# How to get method and selector from NoSuchElementException - c#

I'm using C#.NET 4 and Selenium WebDriver 2.44.0.0 and chromeDriver.
When it cannot find element, Selenium throws error:
no such element
(Session info: chrome=38.0.2125.104)
(Driver info: chromedriver=2.10.267521,platform=Windows NT 6.1 SP1 x86_64)
But I would like to know which element is missing. I see some articles said that it can show details like this:
OpenQA.Selenium.NoSuchElementException: Unable to locate element: {"method":"id","selector":"hello"}
Could anyone tell me how to get the method and selector from NoSuchElementException?
Here is my code
try
{
for(int i=0; i<10; i++)
{
string className = "items-" + i;
IWebElement t = Driver.FindElement(By.CssSelector("[class$='" + className + "'] > span"));
t.Click();
}
}
catch (NoSuchElementException ex)
{
Logger.Error("Error: " + ex.Message);
Debug.WriteLine(ex.Message);
}

The .ToString() method of the By locator returns what you're asking for.
As far as I know the exception itself does not contain the information. However, it is straight forward to handle the exception and associate it with the locator function that was used in the FindElement() method that threw it.
For example, if the element in the 5th iteration cannot be found, the code below will produce this message
Error: no such element
Error: Unable to find element using function By.CssSelector: [class$='items-4'] > span
By locator; // need to declare this outside the scope of the try block
// so it can be accessed in the catch block
try
{
for(int i=0; i<10; i++)
{
string className = "items-" + i;
locatorFunction = By.CssSelector("[class$='" + className + "'] > span")
IWebElement t = Driver.FindElement(locatorFunction);
t.Click();
}
}
catch (NoSuchElementException ex)
{
Logger.Error("Error: " + ex.Message);
Logger.Error("Error: Unable to find element using function " + locatorFunction.ToString());
}

Refer to selenium API doc for .net found here.
But, I don't think it will give you what you want in terms of exception tracking. Print the selector you are using instead which can help you identify the element throwing the error

Related

Why is try catch sometimes ignored?

One example is this code:
try
{
string domain = o.SelectToken("response[" + i + "].domain").ToString();
...
}
catch(Exception)
{
continue;
}
Instead of just going on in the loop("continue"), vs halts and points at string domain = o.SelectToken("response[" + i + "].domain").ToString(); for an System.IndexOutOfRangeException.
Why is that?
You probably have 'break on all exceptions' selected in the Debug>Windows>Exception settings:
https://learn.microsoft.com/en-us/visualstudio/debugger/managing-exceptions-with-the-debugger?view=vs-2019
Unselecting this will let VS proceed.
You can do it by using two ways.
As suggested in MSDN, is to set it up in your Visual Studio (I believe it's 2019)
Debug > Windows > Exception Settings: Search for index and untick.
Please add exception to handle exception in your code..
catch(IndexOutOfRangeException e)
{
// handle it like logging it in file and continue
continue;
}
catch(Exception)
{
continue;
}

TFS Ignore's Console App Failure - Error Code

I have a console application that coded in C# that is used in my TFS release process, if this application encounters an error it will display the error then return a non-zero error code, looks like this:
try
{
//Does stuff...
return 0; //where 0 = success
}
catch (Exception ex)
{
Console.WriteLine("Error in Main: " + ex.Message + "\n" + ex.StackTrace);
for (int i = 0; i < args.Length; i++)
{
Console.WriteLine("Value in args[" + i + "]: " + (i == 3 ? "**********" : args[i]));
}
//Return error state
return 1;
}
It is invoked by a PowerShell script in TFS that looks like this:
# (Does some parsing stuff...)
echo $abc
Start-Process -FilePath $abc-ArgumentList $arguments -NoNewWindow -PassThru -Wait
Now my problem is that sometime the console app fails, it prints the error and exits correctly, but for some reason TFS doesn't don't detect this and considers the step "done":
I'm not really sure what I'm missing, what does my PowerShell script/application need to do to indicate the the TFS that a failure has occurred? I thought the "PassThrough" tag in PowerShell should route the error code up, is that not correct?
Looks like I was confused about the PowerShell part of the equation, I need to capture the error and pass it up to TFS. I found the following solution helpful. Also solved here...
I did something like this:
$Output = Start-Process XYZ...
If($Output.Exitcode -ne 0)
{
Throw "Errorlevel $($Output.ExitCode)"
}

Error: 'The process cannot access the file because it is being used by another process' in Visual Studio c#

I am having some trouble with this error in Visual Studio:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file 'C:\Users\aheij\Desktop\KinectOutput\swipe.txt' because it is being used by another process.
What I Have tried:
I have tried using these codes obtained from other solved StackOverflow questions similar to mine to try to solve the problem - but even that didn't seem to work?
Ive tried checking if the file is in use, but with no success.
I also run Visual Studio as administrator.
The file is not read-only.
The folder is not open, and the file is not being used in any other program when executing the code - at least not that I can see/know of.
The code:
So, to add some context to my code: I am writing some quick gesture detection code to the Kinect c# BodyBasics SDK v2 code freely available. This is a helper method that I added, that gets called in when a person is in view. If that person is executing the gesture, the method writes the frame time and gesture name to a text file.
Half the time, when the code does work, the gesture recognition works well. However, the other half of the time, the code breaks/stops because the writing to file bit causes the error.
Below is my code to see if the person is standing in the neutral position - its a bit waffly, so apologies about that. I have commented 'ERROR' where the error is (unsurprisingly):
private void Neutral_stance(VisualStyleElement.Tab.Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
{
CameraSpacePoint left_hand = joints[JointType.HandLeft].Position;
CameraSpacePoint left_elbow = joints[JointType.ElbowLeft].Position;
CameraSpacePoint left_shoulder = joints[JointType.ShoulderLeft].Position;
CameraSpacePoint left_hip = joints[JointType.HipLeft].Position;
CameraSpacePoint right_hand = joints[JointType.HandRight].Position;
CameraSpacePoint right_elbow = joints[JointType.ElbowRight].Position;
CameraSpacePoint right_shoulder = joints[JointType.ShoulderRight].Position;
CameraSpacePoint right_hip = joints[JointType.HipRight].Position;
double vertical_error = 0.15;
double shoulderhand_xrange_l = Math.Abs(left_hand.X - left_shoulder.X);
double shoulderhand_xrange_r = Math.Abs(right_hand.X - right_shoulder.X);
if (bf != null)
{
TimeSpan frametime = bf.RelativeTime;
string path_p = #"C:\Users\aheij\Desktop\KinectOutput\Punch.txt"; //write to punch file
string path_s = #"C:\Users\aheij\Desktop\KinectOutput\swipe.txt"; //write to swipe file
if (left_hand.Y < left_elbow.Y)
{
if (right_hand.Y < right_elbow.Y)
{
if (shoulderhand_xrange_l < vertical_error)
{
if (shoulderhand_xrange_r < vertical_error)
{
Gesture_being_done.Text = " Neutral";
File.AppendAllText(path_p, frametime.ToString() + " Neutral" + Environment.NewLine); //ERROR
File.AppendAllText(path_s, frametime.ToString() + " Neutral" + Environment.NewLine); //ERROR
}
}
}
}
else
{
Gesture_being_done.Text = " Unknown";
File.AppendAllText(path_p, frametime.ToString() + " Unknown" + Environment.NewLine); //ERROR
File.AppendAllText(path_s, frametime.ToString() + " Unknown" + Environment.NewLine); //ERROR
}
}
}
Any solutions/ideas/suggestions to point me on the right track would be appreciated. I think that it would be good to use the 'using streamwriter' method as opposed to the method I am using here - but I am not sure how? Any help would be appreciated.
Additonal Info: Using Visual Studio 2015; Using windows 10.
Sidenote: I read somewhere that the Windows Search tool in Windows 10 can interfere and cause problems like this so I need to disable it?
As suggested to me I used the Filestream method & ensured the file was closed after use. But, even this still caused the same error.
Thus, I also got rid of having two file-writing actions in rapid succession of each other. I dont know if this is technically right or even true, but based off of this post here: link, my error could be coming up because I am trying to execute the second 'write to text file' line whilst the previous 'write to text file' line is still executing/writing to that same folder & location - hence the clash? Please someone, correct me if I am wrong.
Either way, this seems to have worked.
See below for my edited/corrected method:
private void Neutral_stance(Body body, IReadOnlyDictionary<JointType, Joint> joints, IDictionary<JointType, Point> jointPoints, BodyFrame bf)
{
//cameraspace point joint stuff here again (see original post for this bit leading up to the if statements.)
if (bf != null)
{
TimeSpan frametime = bf.RelativeTime;
string path_s = #"C:\Users\aheij\Desktop\KinectOutput\swipe.txt";
if (left_hand.Y < left_elbow.Y)
{
if (right_hand.Y < right_elbow.Y)
{
if (shoulderhand_xrange_l < vertical_error)
{
if (shoulderhand_xrange_r < vertical_error)
{
Gesture_being_done.Text = " Neutral";
FileStream fs_s = new FileStream(path_s, FileMode.Append); //swipe
byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + " Neutral" + Environment.NewLine);
fs_s.Write(bdatas, 0, bdatas.Length);
fs_s.Close();
}
}
}
}
else
{
Gesture_being_done.Text = " Unknown";
FileStream fs_s = new FileStream(path_s, FileMode.Append);
byte[] bdatas = Encoding.Default.GetBytes(frametime.ToString() + " Unknown" + Environment.NewLine);
fs_s.Write(bdatas, 0, bdatas.Length);
fs_s.Close();
}
}
}
Do let me know if there is any way I can make this more elegant or anything else I should be aware of w.r.t this answer.
The code is based off of the code found here: FileStream Tutorial website

Best way to handle this HttpWebRequest in a For loop?

Question Background:
I am making a call to Amazon's Product Advertising API. This gives back X number of paged results at a time with an node in the XML stating how many pages are available i.e 10. A page number is then specified and the API will responsed with the results for that given page.
Question:
I want to add all of the results from each page to a list that I then send to an MVC view. Currently I'm doing this through a For Loop specifying the upper limit as the maximum number of loops, for example if there are 10 pages available I'm looping round page 1, 2, 3, 4......and so on until 10.
The following code shows how the request is currently being made. Is this ok? It works but I have seen a StackOverflow exception a couple of times when debugging.
for (int i = 1; i <= 10; i++)
{
string requestUrl = "http://amazonApi.com/page"+i;
WebRequest amazonRequest = HttpWebRequest.Create(requestUrl);
try
{
WebResponse response = amazonRequest .GetResponse();
XmlDocument doc = new XmlDocument();
doc.Load(response.GetResponseStream());
XmlDocument xmlDoc = doc;
//Add Xml Document to List.....
AddToList(xmlDoc);
}
catch (Exception e)
{
System.Console.WriteLine("Caught Exception: " + e.Message);
System.Console.WriteLine("Stack Trace: " + e.StackTrace);
}
}

Windows Phone 8: Code for Getting directions running in Emulator but throwing exception in Device

I have been writing a snippet to get direction between two GeoCoordinates in Windows Phone 8. That particular code is running fine in Emaulator but when I deployed and tested it in Device it is throwing 'System.Reflection.TargetInvocationException' . In my appmanifest ID_CAP_MAP and ID_CAP_LOCATION is enabled. Below is the code snippet that I am using
private void RequestDirections()
{
rq.QueryCompleted += routeQuery_QueryCompleted;
if (!rq.IsBusy)
{
List<GeoCoordinate> routeCoordinates = new List<GeoCoordinate>();
routeCoordinates.Add(new GeoCoordinate(48.860339, 2.337599));
routeCoordinates.Add(new GeoCoordinate(48.8583, 2.2945));
rq.Waypoints = routeCoordinates;
rq.QueryAsync();
map.Center = new GeoCoordinate(48.8583, 2.2945); //Center map on last coordinates
}
}
private void routeQuery_QueryCompleted(object sender, QueryCompletedEventArgs<Route> e)
{
try
{
if (e != null)
{
Route theRoute = e.Result;
MapRoute calculatedMapRoute = new MapRoute(theRoute);
map.ZoomLevel = 17;
map.AddRoute(calculatedMapRoute);
//Used Only For Direction List Button
sb.AppendLine("Distance to destination: " + e.Result.LengthInMeters);
sb.AppendLine("Time to destination: " + e.Result.EstimatedDuration);
foreach (var maneuver in e.Result.Legs.SelectMany(l => l.Maneuvers))
{
sb.AppendLine("At " + maneuver.StartGeoCoordinate + " " +
maneuver.InstructionKind + ": " +
maneuver.InstructionText + " for " +
maneuver.LengthInMeters + " meters");
}
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
And the error I am getting is in routeQuery_QueryCompleted method. The QueryCompletedEventArgs<Route> e is thrown as null. Exact error "'e.Result' threw an exception of type 'System.Reflection.TargetInvocationException'"
It would be great if anybody could suggest how to resolve this.
TargetInvocation just means that something was calling reflection Invoke, and the invoked method threw an exception. The real problem is hiding in the .InnerException. So you should catch the exception and inspect the .InnerException to see what is actually going wrong. Also look at the .StackTrace. of the .InnerException to see where it is going wrong.
You can use GeoCoordinateWatcher.
The Windows Phone API has a GeoCoordinateWatcher class which fires a PositionChanged event which can be used to track the user’s location. It is very easy to render the user’s movements on a map via a MapPolyLine, which is a line path which is defined in terms of geocoordinates.
This article explains how to create a run-tracker app using Windows Phone 8

Categories

Resources