Triet Doan
Triet Doan

Reputation: 12085

Prevent Selenium from opening new window

today, I use Selenium to parse data from a website. Here is my code:

    public ActionResult ParseData()
    {
        IWebDriver driver = new FirefoxDriver();
        driver.Navigate().GoToUrl(myURL);
        IList<IWebElement> nameList = driver.FindElements(By.XPath(myXPath));
        return View(nameList);
    }

The problem is, whenever it runs, it opens new window at myURL location, then get the data, and leave that window opening.

I don't want Selenium to open any new window here. Just run at the background and give me the parsed data. How can I achieve that? Please help me. Thanks a lot.

Upvotes: 1

Views: 4588

Answers (2)

user1433852
user1433852

Reputation: 162

Generally I agree with andrei: why use Selenium if you are not planning to interact with browser window? Having said that, simplest thing to do to prevent Selenium from leaving the window open, is to close it before returning from the function:

driver.Quit();

Another option, if the page doesn't have to be loaded in Firefox, is to use HtmlUnit Driver instead (it has no UI)

Upvotes: 1

turdus-merula
turdus-merula

Reputation: 8854

Well, it seems that on each web request you are creating (though, not closing / disposing) a Selenium driver object. As I have said in the comment, there may be better solutions for your problem...

As you want to fetch a web page and extract some data from it, feel free to use:

A web application is not very a hospitable environment for a Selenium driver instance IMHO. Though, if you still want to play a bit with it, make the Selenium instance static and reuse it among requests. Still, if it will be used from concurrent requests (multiple threads running at the same time), a crush is very probable :) You have the option to protect the instance (locks, critical section etc.) but then you will have zero scalability.

Short answer: fetch the data by in another way, Selenium is just for automatic exploration tests as far as I know...

But...

If you really have to explore that website - the source of your data - with Selenium... Then fetch the data using Selenium in advance - speculatively, in another process (a console application that runs in background) and store it in some files or in a database. Then, from the web application, read the data and return it to your clients :)

If you do not have yet the data the client has asked for, respond with some error - "please try again in 5 minutes", and tell the console application (that's running in background) to fetch that data (there are various ways of communicating across process boundaries - the web app and the console app in our case, but you can use a simple file / db for queuing "data requests" - whatever)...

Upvotes: 0

Related Questions