1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C# webBrowser bot posting to Wordpress

Discussion in 'C, C++, C#' started by seeplusplus, Aug 6, 2013.

  1. seeplusplus

    seeplusplus Power Member

    Joined:
    Aug 18, 2008
    Messages:
    511
    Likes Received:
    163
    Hi,

    I need some help. I've written this code, which logs into Wordpress, goes to the 'add new post' page, inserts a post title, and then publishes it.

    This works fine, the problem I'm having is entering the main content for the post.

    Looking at the Wordpress page source, there is a "content" section, but when I try to add content to that, it doesn't work. The post title though does work, it has an ID of 'title'.

    Has anyone any help they could offer me?

    Here is the source, the GUI just has a webBrowser object and a button object.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                webBrowser1.Navigate("http://localhost/wp/wordpress/wp-login.php");
            }
    
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                if (webBrowser1.DocumentText.Contains("Remember Me"))
                {
                    webBrowser1.Document.GetElementById("user_login").SetAttribute("value", "admin");
                    webBrowser1.Document.GetElementById("user_pass").SetAttribute("value", "password1");
                    webBrowser1.Document.GetElementById("wp-submit").InvokeMember("click");
                }
                else if (webBrowser1.DocumentText.Contains("Welcome to WordPress!"))
                {
                    webBrowser1.Navigate("http://localhost/wp/wordpress/wp-admin/post-new.php");
                }
    
                else if (webBrowser1.DocumentText.Contains("Add New Post"))
                {
    
                    webBrowser1.Document.GetElementById("title").SetAttribute("value", "my bots post title");
                    webBrowser1.Document.GetElementById("publish").InvokeMember("click");
                }
                else if (webBrowser1.DocumentText.Contains("Post published."))
                {
                    MessageBox.Show("Post published");
                }
                else
                {
                    MessageBox.Show("Should not hit");
                }
            }
      
    
    Very simple, but I couldn't find the right element to enter the post's main content.

    Thanks for any help.
     
  2. critsab

    critsab Newbie

    Joined:
    Mar 22, 2012
    Messages:
    15
    Likes Received:
    6
    Occupation:
    Software Developer
    Can you provide source code of contect object?
     
    • Thanks Thanks x 2
  3. neutralhatter

    neutralhatter Jr. VIP Jr. VIP Premium Member

    Joined:
    Jun 23, 2010
    Messages:
    430
    Likes Received:
    330
    Try to loop through all elements with a foreach statement, in the document and when you reach the correct number set the attribute.

    hope this helps,
     
    • Thanks Thanks x 1
  4. Chris22

    Chris22 Regular Member

    Joined:
    Sep 29, 2010
    Messages:
    400
    Likes Received:
    1,059
    Why not just use the XMLRPC api?

    There is a nice library for .NET which I have used once, it's called JoeBlogs or something along those lines. Really simple and easy to use.
     
    • Thanks Thanks x 1
  5. fu288

    fu288 Newbie

    Joined:
    Jan 1, 2012
    Messages:
    2
    Likes Received:
    1
    Generally,Content field is a hidden input element,because WP content is in HTML iframe。
    when you submit,Javascript copy iframe?s HTML into hidden input variable。

    So,you must find this hidden input variable through HTTP sniffer tools,such as fildder。

    good,LUCK。
     
    • Thanks Thanks x 1
  6. seeplusplus

    seeplusplus Power Member

    Joined:
    Aug 18, 2008
    Messages:
    511
    Likes Received:
    163

    Yes this was my suspicion. Unfortunately I don't have any more time for this project, but Fiddler etc are something I do need to learn if I want to be able to bot anything harder than standard markup sites.

    Appreciated:)
     
  7. handmadebots

    handmadebots Senior Member

    Joined:
    Nov 8, 2012
    Messages:
    903
    Likes Received:
    204
    Home Page:
    In my opinion you should use watin for C#, for web automation.
     
  8. Piotr__11

    Piotr__11 Junior Member

    Joined:
    Mar 13, 2012
    Messages:
    122
    Likes Received:
    69
    Hello!
    I also created such a application some time ago and this code worked well for me:

    PHP:
    wb.Document.GetElementById("content-html").InvokeMember("click"); //switch to html editor
    wb.Document.GetElementById("content").InnerText postContent;      //fill the html content 

    wb.Document.GetElementById("content-tmce").InvokeMember("click");//switch back to visual editor
    Enjoy!
     
  9. aymenbnr

    aymenbnr Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 14, 2009
    Messages:
    200
    Likes Received:
    75
    Home Page:
    Yup that should work , i was about to say he should change from visual editor to html editor , as tinymce can cause you troubles !
     
  10. botrockets

    botrockets Regular Member

    Joined:
    Mar 16, 2013
    Messages:
    272
    Likes Received:
    463
    Occupation:
    Software Developer
    Location:
    Saint T.N.
    you can use headless browser with same effect of webbrowser