How to access and manipulate a Web page in the same way that the user would do by the browser?

2

I'm planning to make software that posts topics in a forum. The initial screen will have a menu with all the categories and subcategories, when choosing one I will open in the background a link (link to create a new topic), from there the user will be redirected to another screen in which will have the title and content fields of the topic according to the category.

I want to know how to integrate, for example: Take the text of a "Title" field in my program and put it in input with id subject of the html page that is in the background.

Finally, in the submit button I have a Javascript function in onClick , how do the assignment actions and texts in the inputs and then call the function onClick ?

If you can give me what to research, or an example to follow I would be grateful, as I researched and did not find anything like it.

Considering this HTML code:

<!DOCTYPE html>
<html>
    <body>
         <h1> Titulo </h1>
         <form>
             <input type="text" id="subject"/>
             <input onclick="envia();" type="submit" id="envia" value="Enviar"/>
         </form>
    </body>
</html>

How to put text in input with ID subject of the page, and when clicking the button the form call the function envia() of the page?

    
asked by anonymous 17.09.2015 / 19:29

2 answers

1

If it is not a problem for your application to open a browser, you can use Selenium.

I've slightly changed your example web code:

<!DOCTYPE html>
<html>
<body>
    <h1> Titulo </h1>
    <form>
        <input type="text" id="subject" />
        <!--
            Erro ao colocar o Id do botão com o mesmo nome da function do onclick, alterei o nome da function para enviar
            Tirei o type submit e troquei por button para que nao fizesse um submit no form
            <input onclick="envia();" type="submit" id="envia" value="Enviar" />
            
            -->
        <input onclick="enviar();" type="button" id="envia" value="Enviar" />
    </form>
    <script>
        function enviar() {
            var subject = document.getElementById('subject').value;
            alert("Subject: " + subject);
        }
    </script>
</body>
</html>

I made an example using windows forms.

private void button2_Click(object sender, EventArgs e)
    {
        IWebDriver driver = new FirefoxDriver();

        //utilize a url do site que você deseja preencher
        driver.Navigate().GoToUrl("http://localhost:7547/index.html");

        //note que mudei esta linha em relação ao ex. lá ele usa o attribute Name, aqui uso o Id
        IWebElement subject = driver.FindElement(By.Id("subject"));
        subject.SendKeys("Cheese");

        IWebElement button = driver.FindElement(By.Id("envia"));
        button.Click();
    }

Some examples on the Selenium website: link

If you want to do this without opening the browser, look for XVFB Selenium, I do not know if there is a solution for C #.

Use Nuget to add Selenium to your project:

If you want more information about this type of functionality, look for Web Scrapper.

    
22.09.2015 / 20:50
0

I do not know if I understand exactly what you need. If you are doing a windows forms project that behind a website (forum) you can use the WebBrowser :

WebBrowser oWebBrowser = new WebBrowser(); // Instancia o Browser
oWebBrowser.Navigate("https://www.seuforum.com.br"); // Acessa a URL do fórum
oWebBrowser.Document.GetElementById("subject").SetAttribute("value","SEU VALOR"); // muda o texto do input
oWebBrowser.Document.InvokeScript("eval", new object[] { "envia()" }); // invoca a função envia() do html
    
22.09.2015 / 21:18