C# webBrowser bot posting to Wordpress

seeplusplus

Power Member
Joined
Aug 18, 2008
Messages
525
Reaction score
179
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.
 
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,
 
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.
 
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。
 
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。


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:)
 
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!
 
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!

Yup that should work , i was about to say he should change from visual editor to html editor , as tinymce can cause you troubles !
 
Back
Top