Help us understand the problem. What is going on with this article?

Python: selenium でFirefoxを操作する

More than 5 years have passed since last update.

pipで selenium をインストール:

(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...

Firefoxのウェブドライバを作成:

>>> from selenium import webdriver
>>> browser = webdriver.Firefox()

この時点で、 Firefoxが起動されます

Google に移動

>>> browser.get('http://www.google.com')
>>> print browser.title
Google

検索の inputタグ をidで探す

>>> browser.find_element_by_id('lst-ib')
<selenium.webdriver.remote.webelement.WebElement object at 0x1026661d0>
>>> q=_

これにキーワード入力。

>>> q.send_keys('selenium')

この時点でFirefoxでJavascriptが動いてキーワード候補がでている。

リターンを入力すると、javascriptが起動してフォームがSubmitされます。

>>> q.send_keys('\n')

>>> print browser.title
selenium - Google 検索

CSSセレクタで選択

>>> 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 検索

BeautifulSoupでHTMLを印刷

>>> 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>
hidelafoglia
コンピュータプログラミング勉強中です。
https://github.com/hdknr/scriptogr.am/tree/master/qiita
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした