0

I want to run a script using API calls in C#. I don't want the webpage to open and just the script should run. I am trying this:

HttpWebRequest request = WebRequest.Create(URL) as HttpWebRequest;
HttpWebResponse response = request.GetResponse() as HttpWebResponse;

HtmlDocument doc; //I have tried HtmlDocument = new HtmlDocument();, didn't work.
var resultStream = response.GetResponseStream();
doc.LoadHtml(resultStream); // I have tried using Load instead of LoadHtml,didn't work out.
doc.InvokeScript("Submit");

I get an error, use of unassigned variable doc. and doc doesn't contain function name LoadHtml. I have adding the Microsoft.VisualStudio.TestTools.UITesting.HtmlControls; , didn't help.

I have checked th questions HtmlDocument.LoadHtml from WebResponse? and Get HTML code from website in C# but they didn't get an error on doc.

Any solutions.

2 Answers 2

1

You will have to change the way you load the HtmlDocument

string html = new WebClient().DownloadString(URL);
WebBrowser browser = new WebBrowser()
{
    ScriptErrorsSuppressed = true,
    DocumentText = string.Empty
};
HtmlDocument doc = browser.Document.OpenNew(true);
doc.Write(html);
doc.InvokeScript("Submit");

Hope it works.

Sign up to request clarification or add additional context in comments.

3 Comments

I tried it, it gave an error that there is no constructor that accepts 0 arguments.
Are you using HTML Agility Pack?
Check the script name, remember the name is case sensitive. Does this script has any parameters? Maybe it is executing but not stopping due to validations. You may try to dump the html to make sure the script is there.
0
HtmlDocument doc; //I have tried HtmlDocument = new HtmlDocument();, didn't work.

This won't work unless you do:

HtmlDocument doc = new HtmlDocument(/*someUri*/, /*documentLocation*/);

You need to initialize doc. This is why you are seeing

use of unassigned variable doc

Check out the documentation here for details:

https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.webtesting.htmldocument.aspx

According to it, the constructor signature is:

HtmlDocument(Uri, String)

with the description:

Initializes a new instance of the HtmlDocument class. This constructor takes a >string and uses it as the document.

Also, by looking at the documentation, it doesn't have a method LoadHtml()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.