From 7d81f4019b6a156dfa4f455c967837c6fcc76002 Mon Sep 17 00:00:00 2001 From: kev Date: Wed, 23 Sep 2015 11:32:47 +0800 Subject: [PATCH] update --- selenium/README.md | 60 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 50 insertions(+), 10 deletions(-) diff --git a/selenium/README.md b/selenium/README.md index 43a9750..2a0b74f 100644 --- a/selenium/README.md +++ b/selenium/README.md @@ -22,13 +22,19 @@ firefox: $ docker-compose up -d ``` -## Client - -demo.py +> Another way to start selenium server: ``` -#!/usr/bin/env python3 +$ npm install -g selenium-standalone +$ selenium-standalone install +$ selenium-standalone start +``` +## Client + +baidu-search.py + +``` from selenium import webdriver from selenium.webdriver.common.desired_capabilities import DesiredCapabilities @@ -37,16 +43,50 @@ driver = webdriver.Remote( desired_capabilities=DesiredCapabilities.FIREFOX ) -driver.get('https://github.com/SeleniumHQ/docker-selenium') -driver.save_screenshot('before-click.png') -driver.find_element_by_css_selector('.author>a').click() -driver.save_screenshot('after-click.png') +driver.get('http://www.baidu.com/') +driver.find_element_by_id('kw').send_keys('webdriver') +driver.find_element_by_id('su').click() +driver.save_screenshot('baidu.png') +driver.close() +``` + +search-baidu.js + +``` +var webdriver = require('selenium-webdriver'), + By = require('selenium-webdriver').By, + until = require('selenium-webdriver').until, + fs = require('fs'); + +webdriver.WebDriver.prototype.saveScreenshot = function(filename) { + return driver.takeScreenshot().then(function(data) { + fs.writeFile(filename, data.replace(/^data:image\/png;base64,/,''), 'base64', function(err) { + if(err) throw err; + }); + }) +}; + +var driver = new webdriver.Builder() + .forBrowser('firefox') + .usingServer('http://127.0.0.1:4444/wd/hub') + .build(); + +driver.get('http://www.baidu.com/'); +driver.findElement(By.id('kw')).sendKeys('webdriver'); +driver.findElement(By.id('su')).click(); +driver.wait(until.titleIs('webdriver_百度搜索'), 1000); +driver.saveScreenshot('baidu.png'); +driver.quit(); ``` ``` +# PYTHON $ pip3 install selenium -$ python3 demo.py -$ ls *.png +$ python3 baidu-search.py + +# NODEJS +$ npm install -g selenium-webdriver +$ node search-baidu.js ``` [1]: http://seleniumhq.org/