1
2
mirror of https://github.com/vimagick/dockerfiles synced 2024-06-25 16:28:40 +00:00
dockerfiles/selenium/README.md

170 lines
3.4 KiB
Markdown
Raw Normal View History

2015-09-22 16:35:28 +00:00
selenium
========
[Selenium][1] is an umbrella project for a range of tools and libraries that enable
and support the automation of web browsers.
2016-02-04 02:12:14 +00:00
Watch [this][2] video to get started.
2015-09-22 16:35:28 +00:00
## Server
docker-compose.yml
2016-02-04 02:12:14 +00:00
```yaml
2015-09-22 16:35:28 +00:00
firefox:
2016-02-04 02:12:14 +00:00
image: selenium/standalone-firefox-debug
2015-09-22 16:35:28 +00:00
ports:
- "4444:4444"
2016-02-04 02:12:14 +00:00
- "5900:5900"
2015-09-22 16:35:28 +00:00
environment:
- JAVA_OPTS=-Xmx512m
restart: always
```
2016-11-23 07:52:41 +00:00
docker-compose-grid.yml
2016-02-04 02:12:14 +00:00
```yaml
hub:
image: selenium/hub
container_name: hub
ports:
- "4444:4444"
2016-11-23 07:51:17 +00:00
environment:
- GRID_TIMEOUT=60
- GRID_BROWSER_TIMEOUT=30
restart: always
2016-02-04 02:12:14 +00:00
chrome:
image: selenium/node-chrome
container_name: chrome
ports:
- "5555"
links:
- hub
restart: always
2016-02-04 02:12:14 +00:00
firefox:
image: selenium/node-firefox
container_name: firefox
ports:
- "5555"
links:
- hub
restart: always
2015-09-22 16:35:28 +00:00
```
2016-02-04 02:12:14 +00:00
> Access grid console at <http://127.0.0.1:4444/grid/console>
2016-11-23 07:51:17 +00:00
docker-compose-node.yml
```yaml
firefox:
image: selenium/node-firefox
ports:
- "5555:5555"
- "5900:5900"
environment:
- JAVA_OPTS=-Xmx512m
- NODE_MAX_INSTANCES=2
- NODE_MAX_SESSION=2
- SE_OPTS=-host 5.6.7.8 -port 5555
- HUB_PORT_4444_TCP_ADDR=1.2.3.4
- HUB_PORT_4444_TCP_PORT=4444
restart: always
```
2016-02-04 02:12:14 +00:00
```bash
2015-09-22 16:35:28 +00:00
$ docker-compose up -d
```
2015-09-23 03:32:47 +00:00
> Another way to start selenium server:
2016-11-23 07:51:17 +00:00
```bash
2015-09-23 03:32:47 +00:00
$ npm install -g selenium-standalone
$ selenium-standalone install
$ selenium-standalone start
```
2015-09-22 16:35:28 +00:00
## Client
2015-09-23 03:32:47 +00:00
baidu-search.py
2015-09-22 16:35:28 +00:00
2016-11-23 07:51:17 +00:00
```python
2015-09-22 16:35:28 +00:00
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
2019-05-10 10:04:28 +00:00
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
proxy = Proxy({
'proxyType': 'MANUAL',
'httpProxy': '1.2.3.4:8080',
'sslProxy': '1.2.3.4:8080',
})
capabilities = DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
2015-09-22 16:35:28 +00:00
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
2019-05-10 10:04:28 +00:00
desired_capabilities=capabilities
2015-09-22 16:35:28 +00:00
)
2015-09-23 03:32:47 +00:00
driver.get('http://www.baidu.com/')
2019-05-10 10:04:28 +00:00
driver.find_element_by_id('kw').send_keys('ip')
2015-09-23 03:32:47 +00:00
driver.find_element_by_id('su').click()
2019-05-10 10:04:28 +00:00
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'nums_text'))
)
2015-09-23 03:32:47 +00:00
driver.save_screenshot('baidu.png')
driver.close()
2015-09-22 16:35:28 +00:00
```
2015-09-23 03:32:47 +00:00
search-baidu.js
2016-11-23 07:51:17 +00:00
```javascript
2015-09-23 03:32:47 +00:00
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();
```
2016-11-23 07:51:17 +00:00
```bash
2016-02-04 02:12:14 +00:00
# VNC
$ open vnc://:secret@127.0.0.1:5900
2015-09-23 03:32:47 +00:00
# PYTHON
2015-09-22 16:35:28 +00:00
$ pip3 install selenium
2015-09-23 03:32:47 +00:00
$ python3 baidu-search.py
# NODEJS
$ npm install -g selenium-webdriver
$ node search-baidu.js
2015-09-22 16:35:28 +00:00
```
[1]: http://seleniumhq.org/
2016-02-04 02:12:14 +00:00
[2]: https://www.youtube.com/watch?v=S4OkrnFb-YY