Using marigoso to Post a Comment to a Blogger Post

A simple tutorial demonstrating how to use marigoso to automatically launch a browser and post a comment to a blog post.

Install marigoso

Execute the command below to install marigoso.


In [ ]:
pip install -U marigoso

Launch the browser

We can use marigoso Test to launch a browser.


In [3]:
from marigoso import Test
browser = Test().launch_browser("Firefox")

Once the browser is launched it will display a blank page. We then instruct this browser to navigate to the blog post.


In [4]:
browser.get_url("http://pytestuk.blogspot.co.uk/2015/11/testing.html")

Once the browser has visited Blogger, it may or may not display the Cookie Policy of Google. We then use the "press_available" function of the browser to accept that policy if it is available.


In [5]:
browser.press_available("id=cookieChoiceDismiss")

Switch to iFrame

The comment section or comment form of the post is inside an iframe. In order for us to be able to interact with the comment form, we need to use the Selenium function called "switch_to.frame". First we obtain the iframe and then switch into it.


In [6]:
iframe = browser.get_element("css=div#bc_0_0T_box iframe")
browser.switch_to.frame(iframe)

Keyboard Type

After switching to the comment iframe, we can finally interact with the comment form. We grab the comment field and enter anything we would like to say. Here we are simulating the action of typing something in the keyboard to enter into the comment form.


In [7]:
browser.kb_type("id=commentBodyField", "An example of Selenium automation in Python.")

Select an Item from a Dropdown Menu

We then simulate mouse movement for selecting an item in a dropdown menu. You will not see any actual mouse movement in this case however, because we will just grab the dropdown and instruct it to select the item we want.


In [8]:
browser.select_text("id=identityMenu", "Google Account")


Out[8]:
True

After simulating the keyboard typing and dropdown selection, the page should now look like the image below:

Submit the Form

Finally, we submit the form by using the "submit_btn" function, which will simulate pressing the "Publish" button in the page.


In [9]:
browser.submit_btn("Publish")

After clicking the "Publish" button, the page will then be redirected to the Google Sign-in page. What goes beyond here is now an exercise for the reader. Please try it on your own. Write the next code that will automatically log you to your Google Account.

Disposing the browser

Once your finished, you can quit the the browser using the command below.


In [10]:
browser.quit()