i am automating a process on firefox using selenium with c# .NET4.0:
static void Main(string[] args)
{
selenium = new DefaultSelenium("localhost", 4444, "*firefox", "https://www.microsoft.com/");
selenium.Start();
selenium.SetSpeed("900");
selenium.WindowMaximize();
selenium.WindowFocus();
Login();
}
public static void LogIn()
{
verificationErrors = new StringBuilder();
selenium.SetSpeed("900");
selenium.Open("/Login.aspx");
selenium.Type("id=ctl00_cphBody_objLogin_UserName", "user");
selenium.Type("id=ctl00_cphBody_objLogin_Password", "P#assword");
selenium.Click("id=ctl00_cphBody_objLogin_LoginImageButton");
selenium.WaitForPageToLoad("30000");
try
{
selenium.IsTextPresent("Orders - Drug Testing");
}
catch (Exception)
{
Console.WriteLine("Could't find text: Orders - Drug Testing.");
}
}
firefox opens, and then NOTHING happens at all. it does not navigate to the requested page.
here's what it looks like. what am i doing wrong? everything works fine with IE
To navigate to the requested page, need to use the following piece of code.
Selenium doesn't navigate to the page or base url automatically until & unless you call
selenium.open(" < your url goes here > ");
Ex : selenium.open("https://mail.google.com");
Related
I'm trying to launch a specific URL using Firefox. But I'm only able to open Firefox browser and not able to launch that URL.
class BrowserHelper
{
IWebDriver driver;
string path = Path.Combine(Environment.CurrentDirectory, #"gecko\\");
public void Navigate(string url)
{
path = path.Replace(#"\", #"\\");
var driverService = FirefoxDriverService.CreateDefaultService(path);
driverService.HideCommandPromptWindow = true;
if (driver == null)
{
driver = new FirefoxDriver(driverService);
}
driver.Url = url;
driver.Navigate().GoToUrl(driver.Url);
driver.Manage().Window.Maximize();
}
}
class Realtest
{
BrowserHelper BH = new BrowserHelper();
public void test()
{
string search ="apple";
BH.Navigate("https://www.google.com/search?q=" + search);
}
}
And I can only get this page:
Here's the final URL I want to launch: https://www.google.com.sg/search?q=apple
Any suggestions? Thanks in advance.
I have tried the below code (in Java), and it's working fine by launching the browser and loading the URL also.
System.setProperty("webdriver.gecko.driver","Drivers/geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.com.sg/search?q=apple");
So I feel the problem is with geckodriver version and FireFox browser installed in your local machine. I would suggest you update FireFox and geckodriver to the latest version.
There is also a pretty easy solution using the command line with c#.
Simply execute the following command to open a new Firefox Tab with the given URL:
start firefox wikipedia.de
You can also start a new Firefox instance if you wish:
start firefox -new-instance wikipedia.de
Last but not least the .Net Code to execute the commands in CLI:
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo()
{
Arguments = "/c start firefox wikipedia.de",
CreateNoWindow = true,
FileName = "CMD.exe"
});
There is also a lot of other stuff that can be done with Firefox commandLine paramters. You an find all of them here:
https://developer.mozilla.org/en-US/docs/Mozilla/Command_Line_Options?redirectlocale=en-US&redirectslug=Command_Line_Options
This also works with chrome and opera, simply call
start opera wikipedia.de
instead of firefox.
You do not need to set driver.Url, remove that line.
driver.Navigate().GoToUrl(url);
driver.Manage().Window.Maximize();
Also, if you simply want to launch a single URL without interacting with the page, then Selenium is overkill.
Here is my selemium test:
[Test]
public void RunStepsTest()
{
using (var driver = new InternetExplorerDriver())
{
driver.Navigate().GoToUrl(Url);
ExecuteStep(driver, "start");
ExecuteStep(driver, "step1");
ExecuteStep(driver, "step2");
ExecuteStep(driver, "finish");
}
}
private void ExecuteStep(InternetExplorerDriver driver, string stepName)
{
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(x => ExpectedConditions.ElementIsVisible(By.Id(stepName)));
var scrrenshot = driver.GetScreenshot();
scrrenshot.SaveAsFile(Path.Combine(ScreenshotDirectory, stepName + ".jpg"), ScreenshotImageFormat.Jpeg);
var link = driver.FindElement(By.Id(stepName));
link.SendKeys(Keys.Enter);
}
Most of time this test fails on line
scrrenshot.SaveAsFile(Path.Combine(ScreenshotDirectory, stepName + ".jpg"), ScreenshotImageFormat.Jpeg);
with message "Paramter is not valid". What do I do wrong?
In Internet Explorer driver, it's intended to throw this error .
From Github bug tracking :
Because of the limitations of how the IE driver is forced to work in
order to take full-DOM screenshots, screenshots are only supported for
browser windows viewing HTML documents. This is entirely as intended
by the driver (regardless of the behavior of Chrome or Firefox). The
driver is forced by the constraints of the IE browser itself.
Accordingly, I'm closing this as "working as intended".
If you are allowed to use other driver, you can try Firefox or Chrome Driver to have screenshot.
Try this code like this:
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
ss.SaveAsFile("e:\\pande", System.Drawing.Imaging.ImageFormat.Jpeg);
I've read all prior posts and tried all suggestions. I've verified the page is loading via taking a screenshot. It's there. I have a 30 second implicit wait in place, which PhantomJS waits for. I have a fully qualified URL. And i've tested the same simple code snippet below with ChromeDriver and it works by flipping to that driver. Any ideas?
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var d = new PhantomJSDriver();
//var d = new ChromeDriver();
try
{
var s = "abc";
d.Manage().Window.Maximize();
d.Manage().Timeouts().ImplicitlyWait(System.TimeSpan.FromSeconds(10));
d.Navigate().GoToUrl("http://www.google.com");
var e = d.FindElement(By.Name("q"));
e.SendKeys(s);
e.Submit();
Assert.AreEqual<String>(s + " - Google Search", d.Title);
}
catch(Exception e)
{
((ITakesScreenshot)d).GetScreenshot().SaveAsFile("c:\\exception.png", System.Drawing.Imaging.ImageFormat.Png);
Console.WriteLine(d.PageSource);
throw e;
}
finally
{
d.Quit();
}
}
Google may response with different HTML code depending on user agent and/or viewport size. Set a viewport of a desktop browser (like 1280x1024) to get a desktop version, whereas default PhantomJS viewport of 400x300 gets a lighter mobile version.
On how to change viewport size, see https://stackoverflow.com/a/23840494/2715393
I am using Fiddler to catch updates sent to a page via LightStreamer. I am creating a FiddlerCore proxy and connecting a Selenium ChromeDriver instance to it. My automation navigates Chrome to the page, and data comes through the proxy.
Once loaded, the updates to the page (via LightStreamer) visibly appear on the page but they do not come through the AfterSessionComplete event.
If I run the Fiddler desktop application instead of launching my proxy (comment out "StartProxy()") all of the updates (via LightStreamer) come through and appear inside the application. They appear has HTTPS data, but other HTTPS data appears to come through.
I have also using tried the BeforeResponse event instead of AfterSessionCompleted.
Here's my code:
static void Main(string[] args)
{
StartProxy();
var seleniumProxy = new Proxy { HttpProxy = "localhost:8888", SslProxy = "localhost:8888" };
var chromeOptions = new ChromeOptions { Proxy = seleniumProxy };
var path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFilesX86) + "\\ChromeDriver\\";
var chromeService = ChromeDriverService.CreateDefaultService(path);
var driver = new ChromeDriver(chromeService, chromeOptions);
driver.Navigate().GoToUrl("https://mywebpage.com");
// page loads and updates start flowing
Console.ReadLine();
driver.Dispose();
StopProxy();
}
private static void FiddlerApplication_AfterSessionComplete(Session session)
{
// data comes through, just not the LightStreamer Data
var respBody = session.GetResponseBodyAsString();
Console.WriteLine(respBody);
// do something with the response body.
}
static void StartProxy()
{
if (FiddlerApplication.IsStarted())
FiddlerApplication.Shutdown();
FiddlerApplication.AfterSessionComplete += FiddlerApplication_AfterSessionComplete;
FiddlerApplication.Startup(8888, FiddlerCoreStartupFlags.DecryptSSL);
}
static void StopProxy()
{
FiddlerApplication.AfterSessionComplete -= FiddlerApplication_AfterSessionComplete;
if (FiddlerApplication.IsStarted())
FiddlerApplication.Shutdown();
}
Thanks for your help.
Update: I've also tried to use the flag: FiddlerCoreStartupFlags.Default instead of FiddlerCoreStartupFlags.DecryptSSL when starting fiddler with no luck.
SetUpTest:
public void SetupTest()
{
IWebDriver driver = new ChromeDriver();
selenium = new DefaultSelenium(
"localhost",
4444,
"*googlechrome C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
"http://localhost");
selenium.Start();
verificationErrors = new StringBuilder();
}
Test function:
[Test]
public void LoginTest()
{
selenium.Open("http://localhost:8085/");
// login
for (int second = 0; ; second++)
{
if (second >= 60) Assert.Fail("timeout");
try
{
if (IsElementPresent(By.CssSelector("#username"))) break;
}
catch (Exception)
{ }
Thread.Sleep(1000);
}
driver.FindElement(By.Id("username")).SendKeys("admin");
driver.FindElement(By.Id("password")).SendKeys("123456");
driver.FindElement(By.CssSelector(".btn.btn-primary")).Click();
}
private bool IsElementPresent(By by)
{
try
{
driver.FindElement(by);
return true;
}
catch (NoSuchElementException)
{
return false;
}
}
I download Chrome Driver at this link: http://chromedriver.storage.googleapis.com/index.html
The newest version is 2.7.
My chrome version is 31.0.1650.63.
The problem is, driver cannot find element, although it exists in view.
How to make it work?
I'd recommend Selenium IDE and login manually while recording in Selenium IDE. It will help you identifying the correct names of the elements you want to select.
I use something like that
using Selenium;
ISelenium sel = new DefaultSelenium("localhost", 4444, "*firefox", "");
sel.Start();
sel.Open("www.whateveryourwebsideis.com");
sel.Type("id=user_email", "username");
sel.Type("id=user_password", "password");
sel.Click("name=commit");
Update:
seems to me as if you don't use your IDriver to navigate.
You have
selenium.Open("http://localhost:8085/");
but I guess you should use
driver.Navigate().GoToUrl("http://localhost:8085/");
Try
string htmlSource = driver.PageSource;
after loading the page to check if you actually have any HTML to search elements in.
I just tried installing ChromeDriver but it doesn't really work and I don't actually need it, so I'm afraid I have to leave it to you to find a solution...good luck.