Python: Working with Firefox with selenium

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

Python: Working with Firefox with selenium
ScreenShot with Selenium (Python)
Scraping with Selenium [Python]
Scraping with Selenium + Python Part 1
Working with LibreOffice in Python
Working with sounds in Python
Scraping with Selenium in Python
Selenium WebDriver + Firefox49 (provisional) (Python)
Scraping with Selenium + Python Part 2
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Selenium stopped working after updating Firefox
Working with DICOM images in Python
Scraping with selenium
[Python / Selenium] XPath
FizzBuzz with Python3
Get html from element with Python selenium
Scraping with selenium ~ 2 ~
Scraping with Python
Statistics with python
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Scraping with Python
Python with Go
Try working with binary data in Python
Twilio with Python
Integrate with Python
Post Test 3 (Working with PosgreSQL in Python)
Play with 2016-Python
AES256 with python
Working with OpenStack using the Python SDK
Tested with Python
Scraping with Selenium
Working with Azure CosmosDB from Python Part.2
Beginning with Selenium
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
[Python] Automatically operate the browser with Selenium
Practice web scraping with Python and Selenium
Use selenium phantomjs webdriver with python unittest
Operate Firefox with Selenium from python and save the screen capture
Excel with Python
Microcomputer with Python
Python + Selenium + Headless Chromium with aws lambda
Cast with python
Working with GPS on Raspberry Pi 3 Python
Automatic operation of Chrome with Python + Selenium + pandas
Try running Google Chrome with Python and Selenium
Log in to Yahoo Business with Selenium Python
Drag and drop local files with Selenium (Python)
Try working with Mongo in Python on Mac
[Introduction for beginners] Working with MySQL in Python
Serial communication with Python
Zip, unzip with python
Successful scraping with Selenium
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python