1

I'm trying to figure out the ways in Selenium C# to handle the elements on a web page and while trying something new, I don't want to open the browser from scratch for each new try.

So I put a break point in my code at the point right after the page is open. Then I'm trying to write some Selenium code in the Immediate Window. However even a single click call in the Immediate Window gets:

Evaluation requires a thread to run temporarily. Use the Watch window to perform the evaluation.

Google says this is becaue the code I'm trying to run (driver.FindElementByClassName("myClass").Click()) spans a thread and this is not allowed in immediate window. Watch window gives a similar error.

So how can I try something with selenium without starting the browser over and over again?

Cagin Uludamar
  • 372
  • 1
  • 3
  • 16

1 Answers1

0

You can use the C# interactive console to play around with the Selenium API. I found Can the C# interactive window interact with my code?, which lead me to this snippet of code that you can execute right in Visual Studio.

  1. Open the C# interactive console in Visual Studio (View --> Other Windows --> C# Interactive).

  2. Load the assembly for the project that has your Selenium code:

    > #r "C:\path\to\your\project.dll"
    

    You will need to change C:\path\to\your\project.dll to the path to your DLL file on your computer. This will load your project, and all of the project's dependencies, which should also load Selenium if your project declares Selenium as one of its dependencies.

    Really, this is just an arbitrary path to any DLL file on your computer. So you could just load WebDriver.dll and WebDriver.Support.dll from the Selenium related NuGet packages. Just change the path.

    If you load the DLL file for a Visual Studio project, you can also use any type defined in that project, which could include Selenium page model classes.

  3. Add some using directives for common Selenium namespaces:

    > using OpenQA.Selenium;
    > using OpenQA.Selenium.Support.UI;
    

    This will save some typing, and it works just like the using directives at the top of your C# class files.

  4. Initialize and use your web driver:

    > var driver = new OpenQA.Selenium.Chrome.ChromeDriver();
    > var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
    > driver.Manage().Window.Maximize();
    > driver.Navigate().GoToUrl("http://stackoverflow.com");
    > wait.Until(d => { d.FindElement(By.LinkText("About")).Click(); return true; });
    

A code snippet you can copy and paste into the C# interactive console is below:

#r "C:\path\to\your\project.dll"
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
var driver = new OpenQA.Selenium.Chrome.ChromeDriver();
var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
driver.Manage().Window.Maximize();
driver.Navigate().GoToUrl("http://stackoverflow.com");
wait.Until(d => { d.FindElement(By.LinkText("About")).Click(); return true; });

Just change "C:\path\to\your\project.dll" to the real path to your project's DLL file on your computer. This should be one of the bin\X directories in your Visual Studio project's root folder.

Greg Burghardt
  • 17,900
  • 9
  • 49
  • 92