Locate only non-hidden elements using Selenium WebDriver in C# - 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.

Related

How to look for text via XPath using Selenium for C#?

Im trying to achieve checking for the text "In stock.", via a XPath query, however my XPath variable elementInStock returns a ID, and it looks like its not finding the text "In stock.", from the URL https://www.amazon.co.uk/dp/B08H96W864/ref=twister_B08J4RCVXW?_encoding=UTF8&th=1
Would appreciate some help how to find the text "In stock." and do the Console.WriteLine("HEYYYY");
if text "In stock." not found then keep go to the while loop in mu code, my logic does not even go to the while loop if XPATH is null.
Please advise.
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
//Navigate to
driver.Navigate().GoToUrl("https://www.amazon.co.uk/dp/B08H96W864/ref=twister_B08J4RCVXW?_encoding=UTF8&th=1");
driver.Manage().Window.Maximize();
IWebElement elementInStock = driver.FindElement(By.XPath("/html/body/div[2]/div[2]/div[5]/div[4]/div[4]/div[18]"));
if (elementInStock != null)
{
Console.WriteLine("HEYYYY");
}
else if (elementInStock == null)
{
int counter = 0;
while (counter < 10)
{
Thread.Sleep(2500);
driver.Navigate().Refresh();
}
}
}
}
}
I see a few things that might be causing the issues you are seeing and a few other things that will cause issues later. I'll list them here and then below I've made some suggestions for improving the script. Finally, I've updated the script with the suggested improvements. I've tested the code and it's working correctly.
The fact that you don't wait for the element to be visible may be part of the problems you are seeing.
Your script doesn't actually check for the "In stock." text.
There's really no such thing as a null element result of a .FindElement() call. The find either works and returns the element or it doesn't work and it throws an exception. That's why your code never gets to the while loop when the element isn't found. My recommendation here would be to either find an element that you know will always be present or use .FindElements() (plural) and then check to see if there's an element in the returned collection (meaning the element exists). This will avoid unnecessary exceptions being thrown and will still accomplish the task.
Thread.Sleep() takes milliseconds as the parameter so 2500 is 2.5s. I'm assuming you intended to wait for more than 2.5s? I prefer to use TimeSpan.FromSeconds(30) to make it clearer how many seconds I'm waiting. NOTE: There are also many other From* options you may be interested in using also, e.g. TimeSpan.FromMinutes(2).
You never increment counter... meaning your script will be stuck in an infinite loop.
Your XPath is an absolute XPath which means that it starts at the /html tag. This is considered a bad practice because you end up with a very strict (and typically long) XPath which if any element along the specified chain is added/deleted/changed, your XPath will break. If you have to use XPath, instead create a relative XPath with the minimal information needed to uniquely locate the element. Best practice would be to use an ID or CSS selector instead and only use an XPath for locating an element by contained text or for more complex scenarios.
I would suggest a few changes...
In this case, we can use an ID, availability, on the parent element instead of using XPath.
<div id="availability" class="a-section a-spacing-none">
<span class="a-size-medium a-color-success">In stock.</span>
</div>
NOTE: I've cleaned up a LOT of extra whitespace from the HTML above to save space. It won't matter in this case given that I'm planning to use the ID to locate the element but I am going to add .Trim() to the text returned to remove all that extra whitespace.
Add a wait to make sure the element is visible.
Find an element that is always there and check the contained text for the desired string.
Increment counter.
Here's what the final code looks like. I ran this code and it successfully wrote "HEYYYY" to the console.
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;
class Program
{
static void Main(string[] args)
{
IWebDriver driver = new ChromeDriver();
driver.Manage().Window.Maximize();
driver.Url = "https://www.amazon.co.uk/dp/B08H96W864/ref=twister_B08J4RCVXW?_encoding=UTF8&th=1";
int counter = 0;
while (counter < 10)
{
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
IWebElement availability = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("availability")));
if (availability.Text.Trim() == "In stock.")
{
Console.WriteLine("HEYYYY");
break;
}
counter++;
Thread.Sleep(TimeSpan.FromSeconds(30));
driver.Navigate().Refresh();
}
}
}

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

What is the fastest way to see if an element contains a child element?

I have an IWebElement (a div) that contains a child element half of the time. I want to see if it contains the child element and if so, I want to capture it. I do something like this:
IWebElement firstChild;
try
{
firstChild = divElement.FindElement(By.XPath("*"));
}
catch (NoSuchElementException e)
{
// catch and handling...
}
But this is slow on three parts:
1. The use of By.XPath("*")
2. FindElement that is slow in case an element does not exist
3. try/catch is a slower mechanism, I'd rather want a returned boolean on an existing of an item
How can I speed up this way of detecting child elements?
EDIT:
To clarify: The test I am performing runs on grids with divs, a typical grid is 4 x 16, so 64 fields. I want to transform this grid into a DataTable to compare it to the expected outcome. The capturing of these fields to the datatable runs in 22 seconds in total. It is not performing very bad, but I'd like to shave off those precious seconds.
UPDATE:
I managed to do it by capturing the grid with the HTML Agility Pack. Unfortunately (there is always something), the values for the input elements could not be captured because they are dynamically set. As a solution I'd let the HAP return the Ids of the input elements and capture the values with FindElement(By.Id(inputId)) which is blazingly fast compared to other Selenium selection methods.
To keep a long story short: I managed to reduce the capture time from around 22 seconds to less than 3 seconds, a more than 600% performance improvement.
You need to add a . in front of your XPath to search among the descendants:
var elems = divElement.FindElements(By.XPath("(.//*)[1]"));
IWebElement firstChild = elems.Count > 0 ? elems[0] : null;
As an alternative you can use a CSS selector. It might be slightly faster:
var elems = divElement.FindElements(By.CssSelector("*"));
IWebElement firstChild = elems.Count > 0 ? elems[0] : null;
Create an extension method on ISearchContext like this:
public static IWebElement FindElementInstant(this ISearchContext context, By by)
{
Driver.Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.Zero);
var matchingElement = context.FindElements(by).FirstOrDefault();
Driver.Instance.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(/* whatever your implicit wait is normally */));
return matchingElement;
}
Used like:
var firstChild = divElement.FindElementInstant(By.XPath("*"));
If you don't need to access the matching element, you can just return bool out of FindElementInstant.
By temporarily disabling your implicit waiting, FindElements will return an empty collection instantly if no matching elements exist and by using FindElements instead of FindElement within a try-catch you avoid the extra time that comes from exceptions.

Coded UI & Selenium - different execution speed depending on where mouse pointer is

So, working in C#. I have a CSS-based drop-down menu. I open the menu, then grab a reference to it using FindElement by CSSSelector. I then grab the contents of the list using FindElements, again by CSSSelector.
Now here's where it get's interesting. I iterate the list, based on a file I have open in a streamreader.
Looks something like:
list = driver.FindElement(By.CSSSelector("dropdown-menu"));
list_items = list.FindElements(By.CSSSelector("LI > A"));
int row = 0;
while (data_file.read())
{
iWebElement item = list_items[row];
string label = item.text;
string url = item.getattribute("href");
assert.areequal("something", label);
assert.areequal("something else", url);
row++;
}
Now here's the thing: if the mouse pointer is placed over the drop-down, while this is executing, item.text returns value and the test succeeds. If the pointer is anywhere else, item.text will be blank and the test fails. Trying to understand what's going on, and taking a clue from the fact that though the test would fail when running, but would succeed while stepping, I modified the code with a loop:
while (data_file.read())
{
iWebElement item = list_items[row];
string label = item.text;
while (label == "")
{
label = item.text;
}
string url = item.getattribute("href");
assert.areequal("something", label);
assert.areequal("something else", url);
row++;
}
Now the test will always succeed, but if the pointer is not on the control it is SIGNIFICANTLY slower... we're talking a factor of 4 or 5... then when the pointer IS on the control. By wrapping a timer around this, I find that it typically takes between 2 and 4 seconds before .text returns anything but an empty string... sometimes longer.
Again, this delay only seems to apply when the mouse pointer is not over the drop-down. Otherwise, the value appears to be there instantaneously.
Can anyone suggest a possible explanation for why it's behaving this way, and a possible approach to solving it?
BTW, I'm not finding any difference between:
item = list_items[row];
label = item.text;
and
label = list_items[row].text;
Nor does .getattribute("value") produce any faster results than .text.
As for why the menus are acting this way, it's hard to tell with the code you've provided. If you could provide the code that displays your menu, that might help. As for solutions, there are a couple.
The label could be taking longer to display because of the while loop itself - it's just going crazy grabbing the text over and over very quickly. A better solution would be to wait for the element to be present. This may make your code run faster. See the Selenium Website for information on using WebDriverWait.
Alternatively, there is an "ugly" solution. You can just move the mouse to the menu with Selenium, to make sure the menu is always displayed when you need it. I've adapted some code from here as an example:
OpenQA.Selenium.Interactions.Actions builder = new
builder.MoveToElement(list).Build().Perform();
Hope this helps!

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.

Categories