How do I access and manipulate a web page in the same way that the user would access a web page?

Asked

Viewed 4,104 times

2

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

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

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

If you could give me something to research, or an example to follow, I would be grateful, because I researched and found nothing 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 a text on input with ID subject page, and when clicking the button the form call the function envia() page?

  • Dude, I couldn’t quite figure out what you want to do. It’s like editing the question and trying to explain it better?

  • I will edit and put more details

  • 1
  • The post was edited

  • 1

    I understand what you want to do because I’ve already created something similar, but your question is a little confusing. I have no experience in C# but in Java there is a framework called Htmlunit, through it you can fill the fields of an html page, press buttons, etc... I searched in Soen for something similar and found that question.

  • Okay, I’ll take a look, thanks

Show 1 more comment

2 answers

1


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

I changed your example web code a little bit:

<!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: http://www.seleniumhq.org/docs/03_webdriver.jsp#Introducing-the-Selenium-webdriver-api-by-example

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

Use Nuget to add Selenium to your project: inserir a descrição da imagem aqui

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

0

I’m not sure I understand exactly what you need. If you are doing a windows Forms project that from behind accesses 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
  • The problem with this is that my elements do not contain ID and it associates by id

  • if it is not by ID, by which attribute you will find the element?

  • It can be by Name or Class, because they are not all input that have Ids the other answer there seemed to have served, tested but gave an error and did not have time to search

  • if it’s by Name you can use Getelementsbyname instead of Getelementbyid. If it’s by Class you can make use of Getelementsbytagname and then foreach and check which elements have the class you want, using Getattribute("classname")

Browser other questions tagged

You are not signed in. Login or sign up in order to post.