I am trying to execute a selenium test from c#. I have recorded the test on the selenium IDE and have verified it works using the IDE tool. The test is a simple proof of concept. Browse to the united airlines web site, enter some flight info on their search widget and invoke a search. When I run the project everything works except that the click on the search button does not invoke the click as it does when using the Selenium IDE.
Non working line with no errors thrown
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();
Full Code
driver = new FirefoxDriver();
baseURL = "http://www.united.com/";
verificationErrors = new StringBuilder();
enter code here
driver.Navigate().GoToUrl(baseURL + "/web/en-US/default.aspx?root=1");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Origin_txtOrigin")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Origin_txtOrigin")).SendKeys("fort lauderdale, fl");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Destination_txtDestination")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_Destination_txtDestination")).SendKeys("New York/Newark, NJ (EWR - Liberty)");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).Click();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_DepDateTime_Depdate_txtDptDate")).SendKeys("4/7/2014");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_RetDateTime_Retdate_txtRetDate")).Clear();
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_RetDateTime_Retdate_txtRetDate")).SendKeys("4/14/2014");
driver.FindElement(By.Id("ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();
String test = driver.PageSource;
The problem is there are two ctl00_ContentInfo_Booking1_btnSearchFlight on the page. The first is a div, the second is the button you want to click.
Try this instead:
FindElement(By.CssSelector("input#ctl00_ContentInfo_Booking1_btnSearchFlight")).Click();
Related
I am new to Selenium and C#... I am in middle of a selenium application development using C#. I have a drop down menu on a webpage. I want selenium to click on the exact name after clicking the drop down menu. So I did something like this:
C#/NUnit code:
driver.FindElement(By.LinkText("z")).Click;
driver.WaitForElement(By.LinkText("xxxxx"));
driver.FindElement(By.LinkText("xxxxx")).Click();
but when I build my code I get the following error:
'iWebDriver' does not contain definition for 'WaitforElement'
Okay, I can offer you an example, but I don't know c#, But I can give you the example from Ruby Selenium Binding.
driver.manage.timeouts.implicit_wait=20
driver.find_element(:link_text,"z").click;
driver.find_element(:link_text,"xxxxx").click
Since I have given the implicit wait 20, whenever it finds the element via find_element function, it waits for 20 minutes, So your external wait statement is not necessary .
The C# code might be(I don't know C#, I took it from the net and placing it here)
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.FindElement(By.LinkText("z")).Click;
driver.FindElement(By.LinkText("xxxxx")).Click();
It doesn't work the way you shown.
The next code is an example how you should write it:
WebDriverWait watiDriver = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
IWebElement element = watiDriver.Until(x => Driver.FindElement(By.CssSelector("")));
Where Driver is your instance of a webdriver.
If you want to use it as you shown then you need to add an extension method to the webdriver class. To do this, just create a new static class (with any name) and insert the next code into it:
public static IWebElement WaitForElement(this IWebDriver driver, By how)
{
webDriverWait watiDriver = new WebDriverWait(Driver, TimeSpan.FromSeconds(10));
return watiDriver.Until(x => Driver.FindElement(FindElement(how));
}
Hi I am new to Selenium Webdriver. I can successfully open a webpage and find elements on it.
In one case I have noted that there is a link on a page that becomes clickable after a while. In Firebug on the Script tab, I can see the code for the javascript that does the timer function.
But using Selenium Webdriver if I issue:
driver.PageSource
I cannot see the source code for the Javascript. Delaying for 30 seconds before requesting the source makes no difference. I have tried finding it with various By options using:
driver.FindElement
and so on, but it isnt there.
How does firebug manage to find and show the Javascript source code? Is there a way that I can coerce Selenium Webdriver to return all code referenced by the page?
Or is there a better approach?
Thanks for any advice!
EDIT---------------------
I tried the following in Firefox:
Dim Driver2 As IWebDriver = New Chrome.ChromeDriver
Driver2.Url = "http://mypage"
Dim js As IJavaScriptExecutor = TryCast(Driver2, IJavaScriptExecutor)
Dim title As String = DirectCast(js.ExecuteScript("return JSON.stringify(window)"), String)
and I got
Permission denied to access property 'toJSON'
I read that this wont work in firefox so I tried in Chrome, and got
Blocked a frame with origin "http://mypage" from accessing a
cross-origin frame
and from there no solutions because according to this its a security restriction, apparently you can't access an with Javascript
I'm starting to think Im a bit out of my depth here.
PageSource probably doesn't return an exact snapshot of the DOM & etc.
You can instead inspect javascript using driver.executeScript() but the burden of analyzing the return object may be discouraging.
Regardless - Here's a contrived example:
Object result = driver.executeScript("return JSON.stringify(window)");
System.out.println(result.toString());
We have a legacy remote system here that dont have webapi, webservice, ...
then we need to do integration by Selenium.
We need open multiple tabs from one master tab to do the extraction but when changing to desired tab and getting value by a css selector it get always the result from the fisrt tab.
Our system cant be opened on internet then i did the same with Google as an example, the same behaviour happens.
Its a bug or my fault ? Someone can see whats is wrong?
Below is a simplified version without error check, its minimal code.
Thanks a lot.
public static void testab()
{
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("https://www.google.com/ncr");
IWebElement elmTxt = driver.FindElement(By.CssSelector("input#lst-ib"));
elmTxt.SendKeys("google" + Keys.Enter);
IWebElement elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
string strWebStat = elmQtdRes.Text;
IWebElement elmNewsLnk = driver.FindElement(By.CssSelector("a[href*='tbm=nws']"));
elmNewsLnk.SendKeys(Keys.Control + Keys.Return);
IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
elmBdy.SendKeys(Keys.Control + "2");
elmQtdRes = driver.FindElement(By.CssSelector("div#resultStats"));
string strNewStat = elmQtdRes.Text;
Console.WriteLine("Stat for WEB:[" + strWebStat + "]"); // prints stat for web - OK
Console.WriteLine("Stat for NEWS:[" + strNewStat + "]"); // prints stat for web - WRONG
}
WebDriver will not automatically switch focus to a new window or tab, so we have to tell it when to make the switch.
To switch to the new window/tab, try:
driver.SwitchTo().Window(driver.WindowHandles.Last());
Then, when you are finished on the new tab, close it and switch back to the primary window:
driver.Close();
driver.SwitchTo().Window(driver.WindowHandles.First());
Then from here, you can continue your test.
I'll add a quick disclaimer that I do not have Visual Studio installed on the machine I'm using at the moment and haven't tested the code above. This should, however get you in the right direction.
EDIT: Looking at your code a second time, this example above would replace the SendKeys() call to attempt to switch tab focus:
IWebElement elmBdy = driver.FindElement(By.CssSelector("body"));
elmBdy.SendKeys(Keys.Control + "2");
I want to automate mobile web site testing on Android emulator using c# and Appium. There is a simple test scenario I want to automate for the start:
1. Start Browser
2. Find an element
3. Clear it
4. Send keys
I've got a problem with the second step. Every time MSTest tries to execute FindElementById line in the code below, I get the error:
"An element could not be located on the page using the given search parameters."
[TestClass]
public class UnitTest1
{
private DesiredCapabilities _capabilities;
private AndroidDriver _driver;
public void InitializeDriver()
{
Console.WriteLine("Connecting to Appium server");
_capabilities = new DesiredCapabilities();
_capabilities.SetCapability("deviceName", "test_02");
_capabilities.SetCapability(CapabilityType.BrowserName, "Chrome");
_capabilities.SetCapability(CapabilityType.Version, "5.0.1");
_capabilities.SetCapability(CapabilityType.Platform, "Android");
//Application path and configurations
_driver = new AndroidDriver(new Uri("http://127.0.0.1:4723/wd/hub"), _capabilities);
}
[TestMethod]
public void TestMethod1()
{
InitializeDriver();
var element = _driver.FindElementById("com.android.browser:id/url");
element.Clear();
element.SendKeys(#"http://stackoverflow.com/");
}
}
Input string for the method I've got from UIAutomator that is shown below.
I tried several combinations for the FindElementById input method:
"com.android.browser:id/url"
"id/url"
"url"
but no luck.
My environment:
Windows 8.1
Appium 1.3.4.1
ChromeDriver 2.14.313457
Android Device Monitor 24.0.2
Sorry for misleading !!!
In case of testing web apps in browser the elements should be located as usual elements on the web page ( not as some classes like android.widget.EditText and android.widget.Button). So try for example the following and you will see some result:
var element = _driver
.findElementByXPath("//input[#id='lst-ib']");
To get locators you should run the browser on your desktop, open the page and use some tools/extensions like Firebug in Firefox or Firebug Lite in Chrome browser.
Try these 2 statements:
var element = _driver.FindElement(By.Id("com.android.browser:id/url");
driver.findElementsByXPath("//*[#class='com.android.browser' and #index='1']");
Update ! The following approach is not for web testing:
Could you try to find the element using xpath?
#FindBy(xpath="//android.widget.EditText[contains(#resource-id, 'url')]")
So in your case you can try the following:
var element = _driver.findElementByXPath("//android.widget.EditText[contains(#resource-id, 'url')]");
Update: in case of testing web apps (not native) you should use web page locators instead of Android classes.
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