Jump to content

IE Ajax Problem


Recommended Posts

I'm trying to make a script that allows a page to be created in Wordpress. I can get it to login, put in the title, the content, and it will publish it, but if I add in the adding of custom fields (also know as meta fields) it doesn't work.

It will add the first meta, but the "addmetasub" button never works.

$oForm = _IEFormGetObjByName ($oIE, "post")

$oSubmit = _IEGetObjById ($oForm, "addmetasub")
_IEAction ($oSubmit, "click")

The script works fine without the custom fields in it. The source code of the wordpress page is here: http://slexy.org/view/s204LSYw9C . Line 389-405 is the custom field addition code. My autoit source code is below:

#include 
; Main Wordpress Data
$title = "Title"
$content = "Content"
$customnameone = "name1"
$customvalueone = "value1"
$customnametwo = "name2"
$customvaluetwo = "value2"

; Warn User
$answer = MsgBox(4, "Wordpress Automation", "Please make sure you are logged out of your wordpress site before use." & @CRLF & @CRLF &"Ready?")
If $answer = 7 Then
    Exit
EndIf

; Prompt for Needed Info
$url = InputBox("WP URL", "Please type in the Wordpress Dashboard URL." & @CRLF & @CRLF & "Example:" & @CRLF &  "http://example.com/wp-admin/") ; End Slash Required
If @error = 1 Then Exit
$username = InputBox("WP Username", "Please type in your Wordpress Username")
If @error = 1 Then Exit
$password = InputBox("WP Password", "Please type in your Wordpress Password")
If @error = 1 Then Exit

$oIE = _IECreate ($url)
Sleep(1000)

; Login
$oForm = _IEFormGetObjByName ($oIE, "loginform")
$oQuery = _IEFormElementGetObjByName ($oForm, "log")
_IEFormElementSetValue ($oQuery, $username)
$oQuery2 = _IEFormElementGetObjByName ($oForm, "pwd")
_IEFormElementSetValue ($oQuery2, $password)
_IEFormSubmit ($oForm, 0)
_IELoadWait($oIE)


_IELinkClickByText ($oIE, "Add New", 3)
_IELoadWait($oIE)

$oForm = _IEFormGetObjByName ($oIE, "post")

; Input Titlee
$oQuery = _IEFormElementGetObjByName ($oForm, "post_title")
_IEFormElementSetValue ($oQuery,$title)

;Input Content into the HTML Editor
;$oSubmit = _IEGetObjById ($oIE, "edButtonHTML")
;_IEAction ($oSubmit, "click")
;sleep(100)
;$oQuery = _IEFormElementGetObjByName ($oForm, "content")
;_IEFormElementSetValue ($oQuery,$content)

;Input Content into Visual Editor
$oFrame = _IEFrameGetCollection($oIE, 0)
_IEDocWriteHTML($oFrame,$content)

; Add First Meta
$oSubmit = _IEGetObjById ($oForm, "enternew")
_IEAction ($oSubmit, "click")
$oQuery = _IEFormElementGetObjByName ($oForm, "metakeyinput")
_IEFormElementSetValue ($oQuery,$customnameone)
$oQuery = _IEFormElementGetObjByName ($oForm, "metavalue")
_IEFormElementSetValue ($oQuery,$customvalueone)
$oSubmit = _IEGetObjById ($oForm, "addmetasub")
_IEAction ($oSubmit, "click")
sleep(1000)

; Add Second Meta
$oSubmit = _IEGetObjById ($oForm, "enternew")
_IEAction ($oSubmit, "click")
$oQuery = _IEFormElementGetObjByName ($oForm, "metakeyinput")
_IEFormElementSetValue ($oQuery,$customnametwo)
$oQuery = _IEFormElementGetObjByName ($oForm, "metavalue")
_IEFormElementSetValue ($oQuery,$customvaluetwo)
$oSubmit = _IEGetObjById ($oForm, "addmetasub")
_IEAction ($oSubmit, "click")
sleep(1000)

; Publish Page
$oSubmit = _IEGetObjByName ($oIE, "publish")
_IEAction ($oSubmit, "click")
_IELoadWait($oIE)

Sleep(1000)

_IEQuit ($oIE)

Any ideas how to get that addmetasub button working? :/

Edited by gamers600
Link to comment
Share on other sites

Cannot get to your site due to a corporate proxy block, but found that with alot of modern pages some elements only exist in script and get created dynamically in the html.

What this means is that you may need to:

1) Trigger the creation of the element by following a link or some other action that triggers the js to create the object

2) Sleep() regularly - loadwait only waits for a page load, not an ajax call

c) have an object loadwait function to reduce peppering your code with too many naps, eg:

;routine for waiting until an object exists on a webpage
;returns true if the object exists before global timeout variable value, else False, use flag to search by html id or html name
Func bWaitTillObjExists( $sObj, $iTimeout, $bByID )
    local $return = False
    local $iBegin = TimerInit()
    Do
        Sleep(50)
        local $oObj = _iif($bByID,_IEGetObjById( $oIE, $sObj ), _IEGetObjByName( $oIE, $sObj ))
    until isObj($oObj) or ( TimerDiff($iBegin) > $iTimeout ) ;waiting until Object is found max 10 seconds  
    if isObj($oObj) then $return = True
    return $return
EndFunc

Also, depending on the app, you may need to reset the container variable that you are using to search for an object. quite often I need to reset the form object variable as otherwise the newly created form elements are invisible as they did not exist when the form variable was instantiated, only when a field was updated/some action triggered the script.

Link to comment
Share on other sites

To know what elements to work with in the rendered page at the client (after client side scripting is complete) examine the page with a DOM Inspector, like DebugBar for IE or FireBug for Firefox.

:huh2:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...