1
0
mirror of https://github.com/vimagick/dockerfiles.git synced 2024-12-23 01:39:27 +02:00
dockerfiles/selenium/README.md

132 lines
3.1 KiB
Markdown
Raw Normal View History

2015-09-22 18:35:28 +02:00
selenium
========
2024-04-22 07:35:05 +02:00
![](https://www.perfecto.io/sites/default/files/image/2021-10/components.png)
2015-09-22 18:35:28 +02:00
[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 04:12:14 +02:00
Watch [this][2] video to get started.
2015-09-22 18:35:28 +02:00
## Server
2016-02-04 04:12:14 +02:00
```bash
2024-04-22 07:35:05 +02:00
$ cat /etc/docker/daemon.json
{
"default-ulimits": {
"nofile": {
"Name": "nofile",
"Soft": 32768,
"Hard": 32768
}
},
"live-restore": true
}
$ systemctl reload docker
2015-09-22 18:35:28 +02:00
$ docker-compose up -d
2022-01-11 11:33:20 +02:00
$ curl http://127.0.0.1:4444/
2015-09-22 18:35:28 +02:00
```
2015-09-23 05:32:47 +02:00
> Another way to start selenium server:
2016-11-23 09:51:17 +02:00
```bash
2015-09-23 05:32:47 +02:00
$ npm install -g selenium-standalone
$ selenium-standalone install
$ selenium-standalone start
```
2023-11-29 13:12:25 +02:00
> To kill long-lived sessions:
```bash
ENDPOINT=127.0.0.1:4444
MAX_AGE=$((3*60)) # 3 minutes
curl -s "http://${ENDPOINT}/status" |
jq -r --arg age ${MAX_AGE} '.value.nodes[].slots[]|select(.session and (.session.start|sub("\\..*Z";"Z")|fromdateiso8601 < now-($age|tonumber))).session.sessionId' |
xargs -r -t -I {} curl -w '\n' -X DELETE "http://${ENDPOINT}/session/{}"
```
2015-09-22 18:35:28 +02:00
## Client
2015-09-23 05:32:47 +02:00
baidu-search.py
2015-09-22 18:35:28 +02:00
2016-11-23 09:51:17 +02:00
```python
2015-09-22 18:35:28 +02:00
from selenium import webdriver
2024-04-15 10:51:21 +02:00
from selenium.webdriver.chrome.options import Options
2019-05-10 12:04:28 +02: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',
})
2024-04-15 10:51:21 +02:00
options = Options()
options.set_capability('proxy', proxy.to_capabilities())
2015-09-22 18:35:28 +02:00
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
2024-04-15 10:51:21 +02:00
options=options
2015-09-22 18:35:28 +02:00
)
2015-09-23 05:32:47 +02:00
driver.get('http://www.baidu.com/')
2024-04-15 10:51:21 +02:00
driver.find_element(By.ID, 'kw').send_keys('ip')
driver.find_element(By.ID, 'su').click()
2019-05-10 12:04:28 +02:00
WebDriverWait(driver, 10).until(
2024-04-15 10:51:21 +02:00
EC.presence_of_element_located((By.ID, 'content_left'))
2019-05-10 12:04:28 +02:00
)
2015-09-23 05:32:47 +02:00
driver.save_screenshot('baidu.png')
driver.close()
2015-09-22 18:35:28 +02:00
```
2015-09-23 05:32:47 +02:00
search-baidu.js
2016-11-23 09:51:17 +02:00
```javascript
2015-09-23 05:32:47 +02: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 09:51:17 +02:00
```bash
2016-02-04 04:12:14 +02:00
# VNC
2022-01-11 11:33:20 +02:00
$ open vnc://127.0.0.1:5900
2021-10-14 12:41:53 +02:00
$ open http://127.0.0.1:7900
2016-02-04 04:12:14 +02:00
2015-09-23 05:32:47 +02:00
# PYTHON
2015-09-22 18:35:28 +02:00
$ pip3 install selenium
2015-09-23 05:32:47 +02:00
$ python3 baidu-search.py
# NODEJS
$ npm install -g selenium-webdriver
$ node search-baidu.js
2015-09-22 18:35:28 +02:00
```
[1]: http://seleniumhq.org/
2016-02-04 04:12:14 +02:00
[2]: https://www.youtube.com/watch?v=S4OkrnFb-YY