Selenium FindElement working for Firefox but not for IE - c#

So I am starting to learn automation using Selenium and C#, problem is i can navigate to my facebook, google what ever and all works good on firefox. But when using IE browsers it opens the webpage but then throws error 'NoSuchElementException'. I am using same code, one works one does not.
here is IE code
IWebDriver driver = new InternetExplorerDriver(#"C:\folder");
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Name("q")).SendKeys("Hello World");
Here is firefox code
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com");
driver.FindElement(By.Name("q")).SendKeys("Hello World");

This could be timing issue on both browser. You should try using more stable code using WebDriverWait to wait until element is visible before interaction as below :-
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Name("q")));
element.SendKeys("Hello World");

Related

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

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

Selenium ChromeDriver C# - How to send a shortcut browser

How can I send a Chrome shortcut with Selenium ?
I mean shortcuts like Ctrl+S, Ctrl+T or Ctrl+P which has nothing to do with WebElements. I read a lot of similar questions there, but none of the suggested solutions work for me.
Let's say I want to open a new tab (Ctrl+T) on the browser, I tried all the following code without success:
The "standard" way :
IWebElement body = myDriver.FindElement(By.TagName("body"));
body.SendKeys(Keys.Control + "t");
The action way :
Actions action = new Actions(myDriver);
action.SendKeys(Keys.Control + "t").Build().Perform();
The ChromeDriver way 1 :
if(myDriver is ChromeDriver)
{
ChromeDriver chromeDriver = myDriver as ChromeDriver;
chromeDriver.Keyboard.SendKeys(Keys.Control + "t");
}
The ChromeDriver way 2 :
ChromeDriver chromeDriver = myDriver as ChromeDriver;
chromeDriver.Keyboard.PressKey(Keys.Control);
chromeDriver.Keyboard.PressKey("t");
chromeDriver.Keyboard.ReleaseKey(Keys.Control);
chromeDriver.Keyboard.ReleaseKey("t");
Notice that the first way i mentionned worked for me with other WebDriver than Chrome.
I use :
Selenium 3.0.1
ChromeDriver 2.27.440174
And my driver's initialization is really basic :
ChromeOptions options = new ChromeOptions();
this.myDriver = new ChromeDriver(/* my path */, options);
Any ideas?
It seem to be Chromium issue. You cannot use keys combinations with chromedriver, but you still can use JavaScript as alternative:
IJavaScriptExecutor js = myDriver as IJavaScriptExecutor;
js.ExecuteScript("window.open()"); // Open new browser tab like `CTRL + t` do
Unfortunately this issue currently prevents chrome from reacting to shortcuts like Ctrl+T sent by selenium.
I'm using key combinations with Actions just fine. I have been using this code example for years and it works with Chrome, Firefox, and IE.
public void SelectAll()
{
(new Actions(yourDriverInstance)).SendKeys(Keys.Control).SendKeys("a").Perform();
}
Am I missing something???

Disabling popups using C# in Selenium

I apologize in advance if the following question seems to be very basic, but I am very new to Selenium and I really need help.
So what I am trying to do is, I am trying to open a window popup but Chrome browser is blocking it by its own.
I am used the following code:
ChromeOptions options = new ChromeOptions();
options.AddUserProfilePreference("disable-popup-blocking", "true");
IWebDriver driver = new RemoteWebDriver(new Uri("http://path/to/selenium/server"), options.ToCapabilities());
But its throwing me an exception saying:
Unexpected error. System.Net.WebException: The remote name could not be resolved: 'path'.
I have tried this, But didn't help though gave me a rough idea.
Can someone please help?
(Reference: Unblocking popup using Selenium using C#)
Try as below :-
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
DesiredCapabilities capabilities = DesiredCapabilities.Chrome();
capabilities.SetCapability(ChromeOptions.Capability, options);
IWebDriver driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
Edited :- if you want to use ChromeDriver instead of RemoteWebDriver try as below :
ChromeOptions options = new ChromeOptions();
chromeOptions.AddArgument("--disable-popup-blocking");
IWebDriver driver = new ChromeDriver(#"C:\path\chromedriver", options);
Hope it works...:)

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

Selenium Internet Explorer WebDriver NoSuchElement Exception

New to Selenium WebDriver, trying to get my firefox/chrome scripts working in IE, but it can't seem to locate any element.
For example here's the code I use to start the IE webdriver go to google and search the world 'blah'
driver = new InternetExplorerDriver(#"C:\Program Files (x86)\IEdriver");
driver.Navigate().GoToUrl("http://www.google.com");
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));
IWebElement search = wait.Until<IWebElement>((d) =>
{
return d.FindElement(By.Name("q"));
});
search.SendKeys("Blah");
Result: Google loads then test fails with a 'NoSuchElementException' - Unable to find element with the name==q
this works fine in firefox and Chrome.
*Using Visual Studio Ultimate C# and NUnit
Thanks,
Liz

Categories

Resources