selenium wait for flash page load (document.ready) - c#

I want to make screenshot on webpage which is written in flash
For that I have this code:
ChromeOptions options = new ChromeOptions();
IWebDriver driver = new ChromeDriver(options);
...
// I'm clicking on button whicch opens new browser window
driver.FindElement(By.ClassName("click_me")).Click();
Thread.Sleep(1500);
//switch to new window
driver.SwitchTo().Window(driver.WindowHandles.Last());
//maximize it
driver.Manage().Window.Maximize();
//wait for load
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(10);
//and then take screenshot
Screenshot sc = ((ITakesScreenshot)driver).GetScreenshot();
sc.SaveAsFile(String.Format(#"{0}\{1}.{2}", Environment.GetFolderPath(Environment.SpecialFolder.Desktop), Guid.NewGuid(), ScreenshotImageFormat.Png), ScreenshotImageFormat.Png);
Here works everything but wait for load. It takes a screenshot of loading screen. What can i do?

//wait for load
WaitForLoading(driver);
private void WaitForLoading(IWebDriver driver)
{
var javascriptExecutor = (IJavaScriptExecutor)driver;
_wait.Until(webDriver => javascriptExecutor.ExecuteScript("return document.readyState").ToString() == "complete");
}

Related

C# Chrome Driver While Opening At The Top My Screen

This My Code
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true;
ChromeOptions cOptions = new ChromeOptions();
String pathToExtension = #"C:\Users\Evrenus\AppData\Local\Google\Chrome\User Data\Default\Extensions\cfhdojbkjhnklbpkdaibdccddilifddb\3.4.1_0";
cOptions.AddArgument("load-extension=" + pathToExtension);
cOptions.AddArgument("--window-size=2000,1190");
cdriver = new ChromeDriver(service, cOptions);
cdriver.Navigate().GoToUrl(link);
int sayi = 0;
foreach (var item in cdriver.WindowHandles)
{
if (sayi == 1)
{
cdriver.SwitchTo().Window(item).Close();
}
++sayi;
}
When i was trigger this code i see chrome my screen.
This application is twitch chat bot and while i streaming (usually game), viewers can open songs from my computer but my computer ALT + TAB
i try this code on my Form Load Event
Selenium Chrome window running in second monitor screen stealing focus

Selenium screenshot parameter is not valid

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);

chrome webdriver cannot open new tab

I'm trying to open up a new tab in Selenium.WebDriver.ChromeDriver" version="2.21.0.0" but it doesn't open anything, however if I move the debug tracking step back to the line "body.SendKeys(Keys.Control + 't')" to rerun the second time, it works ??
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
IWebElement body = wait.Until(ExpectedConditions.ElementIsVisible(By.TagName("body")));
Thread.Sleep(2000);
body.SendKeys(Keys.Control + 't');
Update: It seems to put a stop on chrome, it does open the tab properly. So instead of using Thread.sleep, just try:
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
js.ExecuteScript("return window.stop");
body.SendKeys(Keys.Control + 't');
To open a new tab with Chrome:
var driver = new ChromeDriver();
driver.Navigate().GoToUrl("http://stackoverflow.com");
// open a new tab and set the context
driver.ExecuteScript("window.open('_blank', 'tab2');");
driver.SwitchTo().Window("tab2");
driver.Navigate().GoToUrl("https://www.google.com");
Use the following code for your problem :
Actions act = new Actions(driver);
act.sendKeys(Keys.CONTROL,"t").build().perform();

Run multiple chrome (profiles) instances for automation task

I want to run multiple chrome instances with different profiles(each profile have their cookies) to do some task simultaneous . For example I want to search on Google with 2 accounts(each account have their proxy).
I use Visual Studio Community 2015.
This is what I do until now (without proxies):
namespace ChromeBot
{
class Program
{
public static object Application { get; private set; }
static void Main(string[] args)
{
//Set specific profile for Google Chrome
var options = new ChromeOptions();
options.AddArguments("user-data-dir=C:/Users/conta/AppData/Local/Google/Chrome/User Data/");
options.AddArguments("--start-maximized");
options.AddArguments("--profile-directory=Profile 1");
//Create the reference for our browser
IWebDriver driver = new ChromeDriver(options);
driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 0, 12));
//Navigate to Google Page
driver.Navigate().GoToUrl("http://www.google.com");
//Find the element
IWebElement element = driver.FindElement(By.Name("q"));
//Perform ops
element.SendKeys("cars");
// Set specific profile for Google Chrome1
var options1 = new ChromeOptions();
options1.AddArguments("user-data-dir=C:/Users/conta/AppData/Local/Google/Chrome/User Data/");
options1.AddArguments("--start-maximized");
options1.AddArguments("--profile-directory=Profile 2");
//Create the reference for our browser 1
IWebDriver driver1 = new ChromeDriver(options1);
driver.Manage().Timeouts().SetPageLoadTimeout(new TimeSpan(0, 0, 0, 12));
//Navigate to Google Page 1
driver1.Navigate().GoToUrl("http://www.google.com");
//Find the element
IWebElement element = driver.FindElement(By.Name("q"));
//Perform ops
element.SendKeys("smartphones");
}
}
}
When run this code open each profile and do nothing..
Any help?
When user launches chrome through chromedriver it opens a new instance of chrome browser and locks user-data-dir. hence if any other instance tries to open with same user-data-dir, the second instance do not responds.
Please launch each chrome instance with different user-data-dir.

How to execute Selenium Chrome WebDriver in silent mode?

When using Chrome Selenium WebDriver, it will output diagnostic output when the servers are started:
Started ChromeDriver (v2.0) on port 9515
I do not want to see these messages, how can I suppress them?
I do this
ChromeOptions options = new ChromeOptions();
options.AddArgument("--silent");
IWebDriver Driver = new ChromeDriver(options);
But diagnostic output is not suppress.
I simply do this
ChromeOptions options = new ChromeOptions();
options.AddArgument("--log-level=3");
IWebDriver driver = new ChromeDriver(options);
Good question, however, I don't know where you got that .AddArgument("--silent"); thing, as that's Chrome's command line switch, not for ChromeDriver. Also, there isn't a Chrome switch called --silent anyway.
Under OpenQA.Selenium.Chrome namespace, there is class called ChromeDriverService which has a property SuppressInitialDiagnosticInformation defaults to false. Basically what you might want to do is to create
ChromeDriverService and pass it into ChromeDriver's constructor. Please refer to the documentation here.
Here is the C# code that suppresses ChromeDriver's diagnostics outputs.
ChromeOptions options = new ChromeOptions();
ChromeDriverService service = ChromeDriverService.CreateDefaultService();
service.SuppressInitialDiagnosticInformation = true;
IWebDriver driver = new ChromeDriver(service, options);
EDIT:
ChromeDriver (not Chrome) has a command line argument --silent, which is supposed to work. SuppressInitialDiagnosticInformation in .NET binding does exactly that. However, it seems only suppress some of the messages.
Here is a closed chromedriver ticket:
Issue 116: How to disable the diagnostic messages and log file from Chrome Driver?
For me no one of previous answers did not help , my solution was:
ChromeDriverService service = ChromeDriverService.CreateDefaultService(driverLocation);
service.SuppressInitialDiagnosticInformation = true;
service.HideCommandPromptWindow = true;
var driver = new ChromeDriver(service, options);
For me the only thing that worked for
selenium-chrome-driver-2.48.2.jar
chromedriver 2.20
selenium-java-2.48.2.jar
was
ChromeOptions options = new ChromeOptions();
System.setProperty("webdriver.chrome.args", "--disable-logging");
System.setProperty("webdriver.chrome.silentOutput", "true");
driver = new ChromeDriver(options);
try this code it will hide browser with "headless" Argument but Chrome ver should > 58
( and even you can hide command prompt window )
IWebDriver driver;
ChromeOptions options = new ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArgument("test-type");
options.AddArgument("--ignore-certificate-errors");
options.AddArgument("no-sandbox");
options.AddArgument("--headless");//hide browser
ChromeDriverService service = ChromeDriverService.CreateDefaultService(#"chromedriverExepath\");
service.SuppressInitialDiagnosticInformation = true;
//service.HideCommandPromptWindow = true;//even we can hide command prompt window (with un comment this line)
options.BinaryLocation = #"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe";
driver = new ChromeDriver(service, options);
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("https://www.example.com");
For anyone finding themselves here wanting a Java solution, there is a thread here:
Selenium chromedriver disable logging or redirect it java
To run Chrome browser with Selenium in console in completely silent mode, you should use this snippet:
options = Options()
options.headless = True
options.add_experimental_option("excludeSwitches", ["enable-logging"])
That trick will suppress any console message from either the Selenium driver or the browser itself, including the first message DevTools listening on ws://127.0.0.1 at the very start.
only add below line
System.setProperty("webdriver.chrome.silentOutput", "true");
output:-
ChromeDriver was started successfully.
Jun 28, 2022 10:38:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
This code works fine for me:
public static IWebDriver Driver { set; get; }
-----
Driver = CreateBrowserDriver();
////////////// Create Driver
private static IWebDriver CreateBrowserDriver()
{
try
{
var options = new OpenQA.Selenium.Chrome.ChromeOptions();
options.AddArguments("--disable-extensions");
options.AddArgument("--headless"); // HIDE Chrome Browser
var service = OpenQA.Selenium.Chrome.ChromeDriverService.CreateDefaultService();
service.HideCommandPromptWindow = true; // HIDE Chrome Driver
service.SuppressInitialDiagnosticInformation = true;
return new OpenQA.Selenium.Chrome.ChromeDriver(service, options);
}
catch
{
throw new Exception("Please install Google Chrome.");
}
}
////////////// Exit Driver
public static void ExitDriver()
{
if (Driver != null)
{
Driver.Quit();
}
Driver = null;
try
{
// Chrome
System.Diagnostics.Process.GetProcessesByName("chromedriver").ToList().ForEach(px => px.Kill());
}
catch { }
}

Categories

Resources