Python uses win32com to insert HTML elements in Baidu space example

Source: Internet
Author: User
Copy CodeThe code is as follows:


From win32com.client import Dispatchex
Import time
Ie=dispatchex ("Internetexplorer.application")

Ie. Navigate ("http://hi.baidu.com/mirguest/creat/blog/")
Ie. Visible=1
While IE. Busy:
Time.sleep (1)

Body=ie. Document.body
# header
For I in Body.getelementsbytagname ("Input"):
If Str (i.getattribute ("id")) = = = "Spblogtitle":
Print "Find title"
I.value= "Autocreatedbypython"
Break

# Editor
For I in Body.getelementsbytagname ("iframe"):
Print "Find iframe"
If Str (i.getattribute ("id")) = = = "Tangram_editor_iframe_tangram__1":
Print "Find"
Break
Iframe=i
Iframe.click ()
Sondoc=iframe.contentwindow.document;
Print Sondoc
Sonbody=sondoc.body
Print Sonbody
For II in Sonbody.getelementsbytagname ("P"):
Print "Find p"
Ii.innerhtml= "Hello,my First Try"
Tmp=sondoc.createelement ("div")
Tmp.innerhtml= "Bye"
Sonbody.insertbefore (TMP,II)

Tmphtml= "Hello 2"
Sonbody.insertadjacenthtml ("BeforeEnd", tmphtml)
'''
Editor.getcontenthtml
'''

# submit
For I in Body.getelementsbytagname ("div"):
If Str (i.getattribute ("id")) = = = "Btn-box":
Print "Find button"
Break

Btnbox=i
J=btnbox.childnodes (0)
J.click ()

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.