Selenium gets very slow at switching between iframe - c#

I am passing through the same problem (not resolved, BTW) on this Java question, but at C#:
Testcase running very slow after opening new window with iFrame
The iframe I am switching to:
<iframe id="iframeXYZ" width="100%" height="2280px" frameborder="0" src="/Pages/Abc.html">
</iframe>
Code to select this iframe:
var frameXYZ = driver.FindElement(By.Id("IdOfMyFrame"));
driver.SwitchTo().Frame(frameXYZ);
//I do my stuff here, then return to the main frame
driver.SwitchTo().DefaultContent();
The class does not have any Thread.Sleep, nor even explicit/implicit waits.
I ran into this information, but even using the ID of the iframe, it did not help:
The webdriver.switchTo().frame() method can take a name or an id. It would
have to search the DOM (Document Object Model) for the name or id. You can
have multiple things on a page with the same name attribute. So using id
attributes tend to be faster. However, if the computer running the browser
is slow and/or there are a lot of attributes in the DOM, it could take a
while to switch frames.
There is also a webdriver.switchTo().frame() method which takes an index.
So if you know it is frame number 1 you can just use
webdriver.switchTo().frame(1). But if the order of the frames may change
this is not helpful. You can also use a WebElement to switch frames. So
sometimes the webdriver.findElement() method will find the frame faster and
switching to this WebElement will be faster.
This is really getting more into optimizing your code to be faster.
Font: https://grokbase.com/t/gg/webdriver/15bh2n599f/switching-between-frames-is-very-slow
I would like to know if this problem is something related to Chrome browser itself, and if it does not have a final solution.

Related

Asp.Net asynchronous loop not firing

I am attempting to send an image to the browser every 16 ms (~60Hz) from a specific file on the drive which is changing constantly (also at ~60Hz). To do this I am using Response.BinaryWrite(). Below is my code (it really is quite simple)
protected void Page_Load(object sender, EventArgs e)
{
MainLoop();
}
private async Task MainLoop()
{
while (true)
{
//Update image to latest from server
Response.ContentType = "image/png";
Response.BinaryWrite(File.ReadAllBytes(Server.MapPath("/Frames/frame.png")));
Response.Flush();
await Task.Delay(16);
}
}
My problem is that it does not refresh. I have double and triple checked that the file is indeed changing, and I have also tried updating a label with the current time in milliseconds. I have found that even when doing that, it does not update at all after the page loads.
If I reload the page it displays both a new image, and a new time, so the issue doesn't seem to be in the Response writing. Rather, it's as if the page is simply ignoring the loop entirely, and only running once through.
If anybody has any advice on alternatives to try (keep in mind video is not an option due to the live nature of this) I would be glad to hear them. Perhaps I am missing something very simple here, but I just can't find it!
Thanks!
Thoughts on the problem:
So the problem is that the page doesn't render updated results every 16ms. We can't solve this issue through server side code (e.g. c# code like your snippet).
I'm sure the while loop is still running but you will always need to refresh the page manually since this is ASP.NET (traditional web app with refresh). Here is a stack overflow answer that can back me up (in case you are skeptical about what I'm saying) refresh page after 3 seconds using c#
I'm not sure what the end goal here is, but I'm assuming you want to render your page with your updated png every 16ms. Basically from my 2 points above, don't resort to trying to update the image through the server side; instead, look into other options through the client side code (e.g. javascript).
Of course, I would try to comment further on where you should look exactly, but I never really dealt with updating an image every 16ms before!
I'm just going to throw in some links that could generate some ideas:
Refresh an image in the browser every x milliseconds
Change image in HTML page every few seconds
http://www.labbookpages.co.uk/web/realtime.html
I hope this can push you along! Let me know how this goes because I'm curious on how you will do this :)
You need to not only get the new image but also ensure the browser does not use the cashed image.
So, there's four steps to the solution:
Create a controller action that returns the current image, but your controller action needs to take an argument. The argument becomes a query string parameter. Each time you call the method, the param value should be different. This way, the browser considers the URL to be new and will not use a cached image. Your url should look something like http://mysite/my controller/getimage?param=12345678927483817
Create a JavaScript method to change the SRC property of the image by pointing it to the URL of your new controller action. Use something that constantly changes for your query parameter. I suggest using the current date time in milliseconds.
After the page loads, set a timer in JavaScript to call the function you created in Step 2.
Eat a cookie, because cookies are good.

Selenium - PhantomJS - Findelements in DOM traversal is slow

For, reasons, I'm trying to recurse through the DOM using Selenium/PhantomJS.
It works but its slow and I dont know why.
Findelements seems to take about 250ms every time.
I've tried zeroing the implicit wait with not much success. I've also tried using the Xpath with no real change.
Here's the code, any suggestions ?
public static void RecurseDomFromTop()
{
DomRecursor( pjsDriver.FindElement( By.TagName( "*" ) ) );
}
public static void DomRecursor( IWebElement node )
{
ReadOnlyCollection<IWebElement> iwes = node.FindElements( By.TagName( "*" ) );
foreach (IWebElement iwe in iwes)
{
DomRecursor( iwe );
}
}
The approach you are taking to compare two doms this way is wrong. Every time you make Selenium request there is a HTTP request created that is sent to the driver, which send it to the browser, which then sends it back to driver and driver back to you language binding. There is a lot of overhead involved in this.
Instead what you should do is use driver.PageSource and get the whole HTML response in a single call. Then later you can use HTML parsing libraries which are at least 10x faster than the approach you are taking now.
Look at below article which uses HtmlAgilityPack for getting DOM data
https://www.codeproject.com/Articles/659019/Scraping-HTML-DOM-elements-using-HtmlAgilityPack-H

How to use page load indicator/progress bar in MVC

Here is my model:
public class Pitchermodels
{
InsideEdEntities ieEntity = new InsideEdEntities();
PitcherProfileEntities ppEntity = new PitcherProfileEntities();
Pitcher5model p5Mod = new Pitcher5model();
Pitcher6model p6Mod = new Pitcher6model();
public Pitcher5model pitcher5(long? _pid)
{
if (p5Mod.exist(_pid) == true)
{
p5Mod.playerinfo = ieEntity.ppsproc_playerinfo(_pid).FirstOrDefault();
p5Mod.ListP5T1 = ppEntity.ppsproc_newP5_T1(_pid).ToList();
p5Mod.ListP5T2 = ppEntity.ppsproc_P5_T2(_pid).ToList();
return p5Mod;
}
else
{
return null;
}
}
public Pitcher6model pitcher6(long? _pid)
{
if (p6Mod.exist(_pid) == true)
{
p6Mod.playerinfo = ieEntity.ppsproc_playerinfo(_pid).FirstOrDefault();
p6Mod.ListP6T1 = ppEntity.ppsproc_P6(_pid).ToList();
return p6Mod;
}
else
{
return null;
}
}
}
Here's my Controller:
public ActionResult AllPitchers(long? _pid)
{
Pitchermodels pMods = new Pitchermodels();
return View(pMods);
}
And here's my View:
#model MVCdodgersplayerinfohub.Models.Pitchermodels
#{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>All Pitcher Reports</title>
<link href="~/LA.ico" rel="shortcut icon" type="image/x-icon" />
</head>
<body>
<div>
#Html.Partial("Pitcher5",Model.pitcher5(Convert.ToInt32(Request.QueryString["_pid"])))
</div>
<p style="page-break-before:always;"></p>
<div>
#Html.Partial("Pitcher6",Model.pitcher6(Convert.ToInt32(Request.QueryString["_pid"])))
</div>
</body>
</html>
The one thing that isn’t so good is that it took maybe 2+ minutes to load, and it appears to the user like it might not even load (the mouse just spins). Is it possible to put in a progress bar? I think most users like progress bars because at least they know it’s working, and about how long it will take.
There are a few things that you need to know about how browsers work:
If you are opening a page, nothing will display until the connection is closed. Images and other things are parsed later by the browser so
they open after the initial page open
There is no way to determine the current state of the page open unless you know the size of the data being received. For that the page has to start sending data and you need to know exactly how large the data is. For instance, a file has a set size for the data so you can attach a progress bar to it using Javascript. All data is processed on the server while the browser is still waiting for a response.
You will need some kind of tracking on the front end (html side). This means that you atleast need to have some kind of basic shell of a page open that can keep track of the transaction. This can be done by loading portions of the website using AJAX and roughly estimating the amount of time this takes to open (and apply this to the progress bar in relation to time). Breaking down a progress bar to loaded segments (ie. 1/4 segments loaded, progress bar is 1/4th) will work in this setup.
The real problem I can see here is that you are looking for a quick solution and not trying to shoot the elephant in the room. 2 minutes is a very long load time for a website, which means that something is happening in your controller that is dominating that amount of time. The amount of time it takes to send HTML is lighting fast in today's internet, so this is not the root of your problem. Here are a few steps that you can follow to help find the issue:
Look at your database calls. Are you calling the database multiple times for a piece of data that you may already have?
Is the data that you are loading very large. Are you loading entire tables rather than just the columns you need? Are you joining very large tables?
If this relies on say, a file to load the data, is there something else using that file and the program is waiting for access?
Look at your collections. Are you sorting, resorting and sorting again? Are you converting between different collection types (ie. LinkedList to Array to List to something else)? If you have large collections you are trying to sort via C#, could you possibly sort them somewhere else, for instance in the query on the database?
Under what circumstances does your page slow down? Are there pages that open much faster despite having the same amount of data? If so, the bottleneck maybe happening in a very specific place.
Are you running this on a slow server, ie. at home? This can have a huge impact on performance.
These are just a few. 2 minutes is far far too long and will get even worse as you develop the website further. For simple websites that juggle simple data collections, there is no way you are hitting 2 minutes, even on a busy day, without some underlying issue. If your website is under heavy use and your code is slow, look at the most common code segments and try to optimize them first; then move on to optimize other, less used code segments.
If you are still having lots of trouble with performance, after optimizing the vast majority of the website, consider using AJAX with partial views. While they wont increase your total performance, it will allow you to open your website in segments, providing a lot more visual feedback telling the user that the page is still loading.
Yes. You can use waiting popup for the loading time in your component. For eg. if you use waiting popup component, call the popup in the .ajaxStart() event and close the waiting popup in AjaxComplete event.
Please check the below ajax events for further reference.
http://api.jquery.com/category/ajax/global-ajax-event-handlers/

Parsing AJAX driven page

I am trying to parse data from a page that is not filled in until after the page is finished loading. Because of this I cannot get a simple solution utilizing
while (wb.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
}
to work. I have tried using the solution found at View Generated Source (After AJAX/JavaScript) in C# but I cannot figure out how to get it to wait for the post-loading data is downloaded. Please help! The data is automatically filled into the page after it is loaded, no user interaction is required. Thanks!
I just found Waiting for WebBrowser ajax content where the answer was to use a timer....I am not sure how to fix this using a timer instead of Thread.Sleep() (which blocks the thread completely), can someone help me understand the proper way to use this with a quick sample code? Thanks again
I am looking into the suggestion of calling the AJAX myself, but I think it would be better to use the timer. I am still looking for help on the subject. Thanks.
Take a look at the page you are dealing with with Firebug for Firefox. There is a "Net" tab which will allow you to see the actual raw data of all subsequent HTTP Ajax requests that are occurring while the page is loading (but after the initial part of the page has loaded).
By looking at this data it is quite likely you will be able to find JSON or other XML data that contains exactly what you are looking for in response to a GET request containing an ID or something of that nature.
Using a 'fake' browser as mentioned in that linked post should be considered a last resort because it will yield the worst performance on your end because you will likely be downloading and parsing a lot more data than necessary.
For my situation the following solved it:
while (wb.ReadyState != WebBrowserReadyState.Complete)
Application.DoEvents();
while (wb.Document.GetElementById(elementId) != null && wb.Document.GetElementById(elementId).InnerHtml == null)
Application.DoEvents();
The second while loop waits until a specified element is populated by the AJAX. In my situation, if an invalid store # is provided in the url, it forwards to a 404-type page. The first condition verified the element still exists on the page, which it won't if it gets sent to the 404 page. The second condition waits until the element is populated.
An interesting thing I found if that after the AJAX populates the page, wb.Document.InnerText and wb.DocumentStream still contain the downloaded html. Only wb.Document.InnterHTML is updated. In my situation I am creating an HtmlAgilityPack HtmlDocument from the results. Because the DocumentStream becomes outdated, I have to recreate my document like this:
htmlDoc.LoadHtml("<html><head><title>" + wb.DocumentTitle + "</title></head><body>" + wb.Document.Body.InnerHtml + "</body></html>");
In my situation I don't care about meta/scripts in the header, so this works. If someone cared about those things, they would obviously need to adapt that line of code for their own use.

Locate only non-hidden elements using Selenium WebDriver in C#

I have a collection of records on a web page, and when a record is clicked, a 'Delete' link is displayed (actually 'unhidden' as its actually always there).
When trying to access this 'Delete' link, I am using its value.
When I use Driver.FindElement, it returns the first Delete link, even though it's hidden, and therefore can't click it (and shouldn't as it is not the right link).
So, what I basically want to do is find only non-hidden links. The code below works, but as it iterates through every Delete link I am afraid it may be inefficient.
Is there a better way?
public class DataPageModel : BasePageModel
{
private static readonly By DeleteSelector = By.CssSelector("input[value=\"Delete\"]");
private IWebElement DeleteElement
{
get
{
var elements = Driver.FindElements(DeleteSelector);
foreach (var element in elements.Where(e => e.Displayed))
{
return element;
}
Assert.Fail("Could not locate a visible Delete Element");
return null;
}
}
}
While I agree with #Torbjorn that you should be weary about where you spend your time optimizing, I do think this code is a bit inefficient.
Basically what is slowing the code down is the back and forth checking of each element to see if its displayed. To speed up the code, you need to get the element you want in one go.
Two options (both involve javascript):
jQuery
Take a look at the different ways to bring jQuery selectors to Selenium (I wrote about it here). Once you have that, you can make use of jQuery's :visible selector.
Alternatively if you know for sure the page already has jQuery loaded and you don't want to do all the extra code, you can simply use ExecuteScript:
IWebElement element = (IWebElement)driver.ExecuteScript("return $('input[value=\"Delete\"]:visible').first().get(0)");
Javascript
If you want to avoid jQuery you can just write a javascript function to do the same thing you are doing now in C#: Get all the possible elements and return the first visible one.
Then you would do something similar:
string script = //your javascript
IWebElement element = (IWebElement)driver.ExecuteScript(script);
You trade of readability with different degrees depending on which option you pick but they should all be more efficient. Of course these all require that javascript be enabled in the browser.

Categories