Selenium ChromeDriver C# how to navigate page before it loads? - c#

I am testing data flow for a client's website. It has advertisements that take substantially longer to load than the data elements of each page that I would like to test with Selenium commands.
I don't have control over the ads and can't silence them.
I would like to navigate each page with clicks prior to the complete page loading. I know that this is possible because I can do it manually using the mouse. However, despite my attempts the stubborn chromeDriver will not begin automation until the entire page is loaded.
I am using C# .Net 4.6.1, chrome32_55.0.2883.75, and Selenium version 3.0.1.0.
Further, I am using the recommended Selenium page object model. I implemented WaitForLoad() like this:
public override void WaitForLoad()
{
_isLoaded = Wait.Until(d =>
{
lock (d)
{
SwitchToSelf();
return PageRegex.IsMatch(Session.Driver.PageSource);
}
});
}
The PageRegex above will work but only after the full page is loaded. Which is frustrating because I can visually see that the text string that the PageRegex is designed to parse is on the screen. This leads me to believe that there is a setting elsewhere, perhaps while I am configuring ChromeDriver, that would enable me to parse the Session.Driver.PageSource prior to it being completely loaded.
This is how I am instancing the ChromeDriver:
var options = new ChromeOptions();
options.AddArguments("test-type");
options.AddArgument("incognito"); // works
options.AddArgument("--disable-bundled-ppapi-flash"); // works! this turns off shockwave
options.AddArgument("--disable-extensions"); // works
options.AddArguments("--start-fullscreen");
string workFolder = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\\SHARED";
options.BinaryLocation = workFolder + #"\Chrome32\chrome32_55.0.2883.75\chrome.exe";
var driver = new ChromeDriver(options);
driver.Manage().Cookies.DeleteAllCookies();
return driver;

To interact with the page before it has finished loading, you can either lower the timeout and catch the exception:
IWebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
driver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromMilliseconds(500));
try {
driver.Navigate().GoToUrl("http://www.deelay.me/5000/http://www.deelay.me/");
} catch (OpenQA.Selenium.WebDriverTimeoutException) { }
// waits for an element
var body = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("body")));
Or you can disable the waiting by setting the page load stategy to none :
var options = new ChromeOptions();
options.AddAdditionalCapability("pageLoadStrategy", "none", true);
IWebDriver driver = new ChromeDriver(options);
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(5));
driver.Navigate().GoToUrl("http://www.deelay.me/5000/http://www.deelay.me/");
// waits for an element
var body = wait.Until(ExpectedConditions.ElementExists(By.CssSelector("body")));

Have you tried with ElementToBeClickable or VisibilityOfElementLocated methods of ExpectedConditions class. I think it should work in the scenario you mentioned.
WebDriverWait wdw = new WebDriverWait(driver, TimeSpan.FromSeconds(120));
wdw.Until(ExpectedConditions.ElementToBeClickable(By.Id("ElementId")));

I would like to navigate each page with clicks prior to the complete
page loading
Do I understand you right that you want to click on some links while the page is still loading?
If you know which elements you are going to click, maybe you can do the same as in this question How to click on an element before the page is fully loaded i'm stuck, it takes too much time until loading completed

Related

Selenium unable to find elements on this site

On the following website: https://netbank.nedsecure.co.za
there is an inputbox for profile number.
However, Selenium is unable to find the element and throws a NoSuchElementException. I have tried what is suggested in other Stackoverflow questions, regarding using a wait to ensure the page has loaded.
Here is some code that fails:
var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var driver = new ChromeDriver(path);
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(20);
driver.Url = "https://netbank.nedsecure.co.za";
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
wait.Until(drv => drv.FindElement(By.Id("ProfileId")));
var profileInput = driver.FindElementById("ProfileId");
The page had a <frameset> with a <frame> element.
The issue is resolved by switching the driver to the frame
driver.SwitchTo().Frame("frameMain");
Profile Number, PIN & Password & Login button are present on a frame. You need to switch on a frame before performing any action on it. Please use below code to switch on your frame.
driver.SwitchTo().Frame(driver.FindElement(By.id("frameset")));

Headless Browser C# and Alternatives

currently I have the following code using selenium and phantomjs in c#:
public class Driver
{
static void Main()
{
using (var driver = new PhantomJSDriver())
{
driver.Navigate().GoToUrl("https://www.website.com/");
driver.Navigate().GoToUrl("https://www.website.com/productpage/");
driver.ExecuteScript("document.getElementById('pdp_selectedSize').value = '10.0'"); //FindElementById("pdp_selectedSize").SendKeys("10.0");
driver.ExecuteScript("document.getElementById('product_form').submit()");
driver.Navigate().GoToUrl("http://www.website/cart/");
Screenshot sh = driver.GetScreenshot();
sh.SaveAsFile(#"C:\temp\test.jpg", ImageFormat.Png);
}
}
}
My objective is to be able to add a product to my cart and then checkout automatically. The screenshot is just included to test whether the code was successfully working. My first issue is that I often get an error that it cannot find the element with product id "pdp_selectedSize". I'm assuming this is because the the webdriver hasn't loaded the page yet, so I'm looking for a way to keep checking until it finds it without having to set a specific timeout.
I'm also looking for faster alternatives to use instead of a headless browser. I used a headless browser instead of http requests because I need certain cookies to be able to checkout on the page, and these cookies are set through javascript within the page. If anyone has a reccommendation for a faster method, it would be greatly appreciated, thanks!
For your first question, it would behoove you to look into using ExpectedConditions' which is part of theWebDriverWaitclass inSelenium`. The following code sample was taken from here and only serves as a reference point.
using (IWebDriver driver = new FirefoxDriver())
{
driver.Url = "http://somedomain/url_that_delays_loading";
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement myDynamicElement = wait.Until<IWebElement>(d =>
d.FindElement(By.Id("someDynamicElement")));
}
More on WebDriverWaits here.
As to your second question, that is a really subjective thing, in my opinion. Headless Browsers aren't necessarily any faster or slower than a real browser. See this article.

Ocasional InvalidElementStateException or ElementNotVisibleException when calling SendKeys on input

I have this code:
IWebDriver driver = new FirefoxDriver();
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(30));
//Go to the powerbi site
driver.Navigate().GoToUrl("https://powerbi.microsoft.com/en-us/");
//Go to the page with login form
driver.FindElement(By.XPath("html/body/div[2]/header/nav/div/ul[3]/li[1]/a")).Click();
//Fill in email field
driver.FindElement(By.XPath("//*[#id='cred_userid_inputtext']")).SendKeys("example#gmail.com");
When I launch this code on my computer everything works fine without errors. But when I launch this code on my boss's computer, sometimes it works and sometimes it doesn't.
When an error occurs, it's always on the last line of code. I don't remember exactly which error it is: InvalidElementStateException (when the target element is not enabled) or ElementNotVisibleException (when the target element is not visible).
I suppose the whole thing lies on the Click() method. The documentation says:
Click this element. If the click causes a new page to load, the Click() method will attempt to block until the page has loaded.
I don't quite understand how it attempts to block.
Seems like your input isn't always ready immediately after you click the button.
You should wait for it before sending the keys:
Try this instead of your last line:
WebDriverWait wait = new WebDriverWait(driver, new TimeSpan(0,0,5));
var input = wait.Until(ElementIsClickable(By.XPath("//*[#id='cred_userid_inputtext']")));
input.SendKeys("example#gmail.com");
In order to do that check, you should create a ElementIsClickable function, which returns a delegate, just as this answer shows:
public static Func<IWebDriver, IWebElement> ElementIsClickable(By locator)
{
return driver =>
{
var element = driver.FindElement(locator);
return (element != null && element.Displayed && element.Enabled) ? element : null;
};
}
Also, keep in mind that for using the WebDriverWait class you might need to import the Selenium.WebDriver and Selenium.Support packages into your project, just as this answer suggests.
It's coming occasional because sometimes it takes time to load element from DOM and which cause exception like InvalidElementStateException or ElementNotVisibleException.
I ran your snippets code.
It runs fine, try to separate action and find element.
WebElement element = driver.findElement(By.xpath(".//xyz"/);
element.sendkeys("data");
This could work.

Switching tabs not working in Selenium Webdriver

I wrote this code in C# and it's not working for me.
Second url is still opening in the first tab, although I switched tabs and updated handle.
// open first page in first tab
string firstPageUrl = "http://google.com";
driver.Navigate().GoToUrl(firstPageUrl);
// getting handle to first tab
string firstTabHandle = driver.CurrentWindowHandle;
// open new tab
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "t").Build().Perform();
string secondTabHandle = driver.CurrentWindowHandle;
// open second page in second tab
driver.SwitchTo().Window(secondTabHandle);
string secondPageUrl = "http://bbc.com";
driver.Navigate().GoToUrl(secondPageUrl); // FAILED, page is opened in first tab -.-
Thread.Sleep(2000); // slow down a bit to see tab change
// swtich to first tab
action.SendKeys(OpenQA.Selenium.Keys.Control + "1").Build().Perform();
driver.SwitchTo().Window(firstTabHandle);
Thread.Sleep(1000);
action.SendKeys(OpenQA.Selenium.Keys.Control + "2").Build().Perform();
I'm using latest Chrome driver.
Any ideas?
EDIT:
This is not duplicate. I am not opening links here, I want to navigate to URL I've provided. Also, I am using CurrentWindowHandle as suggested, but it is not working for me.
string secondTabHandle = driver.CurrentWindowHandle; is still returning the first tab, even if the second tab cover the first one. Try this
string firstTabHandle = driver.getWindowHandle();
Actions action = new Actions(driver);
action.SendKeys(OpenQA.Selenium.Keys.Control + "t").Build().Perform();
// switch to the new tab
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(firstTabHandle))
{
driver.switchTo().window(handle);
}
}
// close the second tab and switch back to the first tab
driver.close();
driver.switchTo().window(firstTabHandle);
Not entirely sure if this is related to your problem (but I think it might be):
I've found, with selenium, that a better alternative to using Thread.Sleep() is using:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(15));
where first argument is the selenium driver you're using, and the second argument is how long you want to wait before giving up (the timeout)
You can then call:
wait.Until(condition);
where condition is what you want to wait for. There is a selenium class that provides various useful conditions called ExpectedConditions, for example:
ExpectedConditions.ElementToBeClickable(By by)
which would wait for a particular clickable element to be ready to click.
In fact, I've found that any time the content of the page changes, it's a good idea to wait for the element you're using next like this.

Selenium-WebDriver opened the new Firefox window, but didn’t navigate to URL

My source code is copied from selenium docs site. I didn’t make any change.
http://docs.seleniumhq.org/docs/03_webdriver.jsp#user-input-filling-in-forms
I installed Selenium library via NuGet, including
Selenium Remote Control(RC), Selenium WebDriver Mono, Selenium WebDriver Support Classes, Selenium WebDriver, and Selenium WebDriver-backed Selenium.
When I run this code, a new Firefox window was opened. But the Firefox doesn’t navigate to the URL, it just stuck, nothing was loaded.
I tried the Firefox v27, v29, v30 and v31, none of them worked.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
IWebDriver driver = new FirefoxDriver();
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("Cheese");
// Now submit the form. WebDriver will find the form for us from the element
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);
//Close the browser
driver.Quit();
}
}
I have had the same problem. the solution is simply uninstall your current firefox browser and download older version "i tried 2010 version 3.6.10 works great". your code is fine the problem is Mozilla people have decided to not give the right to any third party application to control Firefox.
good luck

Categories

Resources