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
Related
I al looking to capture network traffic and log responses/headers however I cannot seem to find any resources for achieving this in c#. Most if not all of the guides have been for JS or Python. I read that this functionality was not added into the c# version as of 2019 but no new news has been posted. Does anyone know how to get network logs from a ChromeDriver?
Wrong, in selenium 4 you can do that but it is still in beta
Through much effort and googling around I have created something in C# that seems to do what you want. My answer is an adaption of some C# and Java implementations found on the internet which I could not get to work on individually, but did provide some needed insight.
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.DevTools;
using Network = OpenQA.Selenium.DevTools.V108.Network;
using DevToolsSessionDomains = OpenQA.Selenium.DevTools.V108.DevToolsSessionDomains;
internal class Program
{
private static async Task Main(string[] args)
{
string url = "https://stackoverflow.com/questions/68582545/catching-network-traffic-with-selenium-c-sharp";
Uri uri = new Uri(url);
var driver = new ChromeDriver();
var devTools = (IDevTools)driver;
IDevToolsSession session = devTools.GetDevToolsSession();
var domains = session.GetVersionSpecificDomains<DevToolsSessionDomains>();
domains.Network.ResponseReceived += ResponseReceivedHandler;
Task task = domains.Network.Enable(new Network.EnableCommandSettings());
task.Wait();
driver.Navigate().GoToUrl(uri);
var name = Console.ReadLine();
Console.WriteLine("finished");
driver.Dispose();
void ResponseReceivedHandler(object sender,Network.ResponseReceivedEventArgs e)
{
Console.WriteLine("New Work Response received");
Console.WriteLine($"Response Status: {e.Response.Status}");
Console.WriteLine($"Status Text: {e.Response.StatusText}");
Console.WriteLine($"Response MimeType: {e.Response.MimeType}");
Console.WriteLine($"Response Url: {e.Response.Url}");
}
}
}
I hope this answer answers your question or at least helps someone in a similar situation.
You can use this approach in any selenium version and not wait for the Selenium 4 release.
https://www.automatetheplanet.com/webdriver-capture-modify-http-traffic/
If you want to do use a simular version like that one from Jeppe Holt, for Selenium 4+ without being version and browser specific try that one: (Works on Chrome and Edge with Selenium 4.8)
public void SetupNetworkLogging(IWebDriver driver)
{
NetworkManager manager = new NetworkManager(driver);
manager.NetworkResponseReceived += ResponseHandler;
manager.StartMonitoring();
}
private void ResponseHandler(object sender, NetworkResponseReceivedEventArgs e)
{
Console.WriteLine($"Http status: {e.ResponseStatusCode} : {e.ResponseBody} | Url: {e.ResponseUrl} ");
}
You can also experiment with a devtools session:
IDevTools devTools = driver as IDevTools;
var session = devTools.GetDevToolsSession();
var network = session.Domains.Network;
network.EnableNetwork();
network.EnableNetworkCaching();
So I've been trying to open IE in Private using selenium (C#), this is the closest i've come so far:
InternetExplorerOptions op = new InternetExplorerOptions();
op.PageLoadStrategy = PageLoadStrategy.Normal;
op.IgnoreZoomLevel = true;
op.InitialBrowserUrl = "https://entry.wgrintra.net/schadenwv/servlet/main";
op.ForceCreateProcessApi = true;
op.BrowserCommandLineArguments = "-private";
IWebDriver driver = new InternetExplorerDriver(op);
The problem here is, after 60 seconds of opening the browser (correctly in private) the driver times out (last step doesn't finish).
I've looked around a lot, most is just using Capabilities which are not useful anymore.
(I had to add a value to the registry to be able to forcecreate the process api)
Try to refer code example below and make a test with it on your side may help to solve your issue.
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
/**
* Created by Amol Chavan on 9/19/2016.
*/
public class PrivateBrowsing {
public static void main(String args[]){
createInstance();
}
public static WebDriver createInstance(){
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
capabilities.setCapability(InternetExplorerDriver.FORCE_CREATE_PROCESS, true);
capabilities.setCapability(InternetExplorerDriver.IE_SWITCHES, "-private");
System.setProperty("webdriver.ie.driver","C:\\Grid\\IEDriverServer.exe");
WebDriver driver = new InternetExplorerDriver(capabilities);
driver.get("http://www.google.com");
return driver;
}
}
Reference:
How to Open Internet Explorer Browser in Incognito / Private mode using Selenium / WebDriver ?
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);
This code is in a loop to get a screenshot, however I get a blank screen, since a new window opens each time.
How do I minimize the new window so get a screen capture of the background window?
static string TakeScreenshot(string _getScreenShotName)
{
try
{
IWebDriver driver = new InternetExplorerDriver(#"C:\myCSharp\mySelenium\mySelenium\");
Screenshot sss = ((ITakesScreenshot)driver).GetScreenshot();
sss.SaveAsFile(string.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.jpeg", _getScreenShotName, DateTime.Now), System.Drawing.Imaging.ImageFormat.Gif);
System.Console.WriteLine(_getScreenShotName);
return _getScreenShotName;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
throw;
}
}
You should use
//this will maximize the browser and also helps you to set focus on actual browser not on IEDriverServer exe
IWebDriver driver = new InternetExplorerDriver(#"C:\myCSharp\mySelenium\mySelenium\");
driver.Navigate().GoToUrl("some url");
driver.Manage().Window.Maximize();
Unfortunately, there is currently no way to minimize the browser if you want that to do unless using native C# mechanism.
Refer to this
As 'Pierre-Luc Pineault' has already mentioned in the comment,
Use the driver to navigate to a site first before taking the screenshot (as given below).
IWebDriver driver = new InternetExplorerDriver(#"C:\myCSharp\mySelenium\mySelenium\");
driver.get("http://www.google.com");
Screenshot sss = ((ITakesScreenshot)driver).GetScreenshot();
sss.SaveAsFile(string.Format("{0}_{1:yyyy-MM-dd_HH-mm-ss}.jpeg", _getScreenShotName, DateTime.Now), System.Drawing.Imaging.ImageFormat.Gif);
System.Console.WriteLine(_getScreenShotName);
return _getScreenShotName;
You can not use the driver's GetScreenshot method to take the desktop screenshot. If that is what you want to do, please refer to this answer
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");