This article is the 11th day article of Selenium / Appium Advent Calendar 2014. Yesterday's article was @ totutiteta's Robot drives on the web: Robot Framework & Selenium2Library.
Today I've summarized how to work with Firefox using Selenium's Python binding library selenium. I also made a library to operate using selenium.
At the time of writing this article, the latest version of selenium is 2.44.0. (Probably the following code will not work with versions less than 2.35)
First, try starting the browser. This time, we are targeting Firefox.
from selenium.webdriver import FirefoxProfile
default_profile = {
'security.warn_entering_secure': False,
'security.warn_entering_secure.show_once': True,
'security.warn_entering_weak': False,
'security.warn_entering_weak._show_once': True,
'security.warn_leaving_secure': False,
'security.warn_leaving_secure.show_once': True,
'security.warn_leaving_weak': False,
'security.warn_leaving_weak._show_once': True,
'security.warn_submit_insecure': False,
'security.warn_viewing_mixed': False,
'security.warn_viewing_mixed.show_once': True,
}
profile = FirefoxProfile()
for name, value in default_profile.items():
profile.set_preference(name, value)
The Proxy object is set with the ftp_proxy, ssl_proxy, http_proxy attributes. None seems to be good when not in use.
from selenium.webdriver import Proxy
proxy = Proxy()
proxy.ftp_proxy = proxy.ssl_proxy = proxy.http_proxy = None
from selenium.webdriver import Firefox
browser = Firefox(firefox_profile=profile, proxy=proxy)
browser.implicitly_wait = 10 #Set the waiting time until page is loaded
browser.delete_allcookies() #Erase all cookies
Now, let's operate the launched browser.
url = 'http://qiita.com/'
browser.get(url)
tag = browser.find_element_by_id('elemet-id')
tags = browser.find_elements_by_class_name('class-name')
Below you will get a list of input tags.
tags = browser.find_elements_by_css_selector('input')
tag.click()
form = browser.find_elements_by_tag_name('form')[0]
for tag in form.find_elements_by_tag_name('input'):
type_ = tag.get_attribute('type')
if type_ == 'submit':
entry.submit()
textboxes = [tag for tag in browser.find_elements_by_tag_name('input') if tag.get_attribute('type') == 'text']
textbox = textboxes[0]
textbox.send_keys('CHARACTOR')
from selenium.webdriver.common.keys import Keys
textbox.send_keys(Keys.BACK_SPACE) #Delete one character
textbox.send_keys(Keys.BACK_SPACE * 10) #Delete 10 characters
browser.maximize_window()
if tag.is_displayed():
print(u'Is displayed')
else:
print(u'Not displayed')
Below, the screen is scrolled down.
browser.execute_script('window.scrollTo(0, 1000)')
You can do various operations, but in order to actually use the above, you will often want to make various transitions. In some cases, you may want to keep what you are doing from what page you are currently on, the state of the page, or the state of the previous page. (I actually encountered such a situation)
So, I made a library that can be used like a framework.
pywad is a library that allows you to describe startup and control in a standard way.
Since Selenium is used, Selenium must be installed.
$ pip install pywad
pywad has a Runner class and a Part class. The Part class is a class for describing the control of small control units such as one page, and operates as a part of the Runner class.
The Part class has an is_target () method and a run () method. is_target () is a method that determines whether it is a controlled object, and if this method returns True, the run () method will be executed. When actually using it, it will be used by overriding is_target () and run (). Both methods are passed a browser object and an object to hold the status, so use it to define the process.
from pywad.part import Part
from pywad.decorator import url_match
class GoogleTop(Part):
def _is_search_button(self, text):
for word in self.search_words:
if word in text:
return True
@url_match('www\.google\.')
def is_target(self, browser, status):
return True
def run(self, browser, status):
entries = browser.find_elements_by_css_selector('input')
for entry in entries:
if entry.get_attribute('type') == 'text':
entry.send_keys('test\n\n')
The Runner class manages data that needs to be spanned between browsers and parts. By registering the Part class in the Runner class, the target Part is searched for and executed.
from pywad.runner import Runner
def main():
url = 'http://www.google.com'
runner = Runner()
runner.append(GoogleTop())
runner.run(url)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
from pywad.runner import Runner
from pywad.part import Part
from pywad.decorator import url_match
class GoogleTop(Part):
def _is_search_button(self, text):
for word in self.search_words:
if word in text:
return True
@url_match('www\.google\.')
def is_target(self, browser, status):
return True
def run(self, browser, status):
entries = browser.find_elements_by_css_selector('input')
for entry in entries:
if entry.get_attribute('type') == 'text':
entry.send_keys('test\n\n')
def main():
url = 'http://www.google.com'
runner = Runner()
runner.append(GoogleTop())
runner.run(url)
if __name__ == '__main__':
main()
Who will be tomorrow? No one has registered .oO (I want someone to write)
Tomorrow is ***. I can't say that, so I'll put a link to the Advent calendar. http://qiita.com/advent-calendar/2014/selenium
Then ヾ (・ д ・) Byё ヾ (・ д ・) Byё
Recommended Posts