Install selenium with pip:
(tact)PeekoOne:~ hide$ pip install selenium
Downloading/unpacking selenium
Downloading selenium-2.40.0.tar.gz (2.5Mb): 2.5Mb downloaded
Running setup.py egg_info for package selenium
Installing collected packages: selenium
Running setup.py install for selenium
Successfully installed selenium
Cleaning up...
Create Firefox web driver:
>>> from selenium import webdriver
>>> browser = webdriver.Firefox()
At this point Firefox will be launched
Go to google
>>> browser.get('http://www.google.com')
>>> print browser.title
Google
Search for search input tag by id
>>> browser.find_element_by_id('lst-ib')
<selenium.webdriver.remote.webelement.WebElement object at 0x1026661d0>
>>> q=_
Enter keywords in this.
>>> q.send_keys('selenium')
At this point, Javascript is running in Firefox and keyword candidates are displayed.
When you enter the return, javascript will be launched and the form will be submitted.
>>> q.send_keys('\n')
>>> print browser.title
selenium --google search
Select with CSS selector
>>> browser.find_elements_by_css_selector('input[name=q]')
[<selenium.webdriver.remote.webelement.WebElement object at 0x102a8c890>]
>>> q=_
>>> q[0].send_keys('Django\n')
>>> print browser.title
Django-Google Search
Print HTML with Beautiful Soup
>>> from bs4 import BeautifulSoup as Soup
>>> browser.get('http://twitter.com')
>>> print Soup(browser.page_source).select('form')[1].prettify()
<form action="/sessions/change_locale" class="language" method="POST">
<input name="lang" type="hidden"/>
<input name="redirect" type="hidden"/>
<input name="authenticity_token" type="hidden" value="4ae7df1ac2b69b61e5d260c1e4b7ccd29112fcdf"/>
</form>
Recommended Posts