docker + selenium实操

(1)  docker pull elgalu/selenium #upgrades to latest if a newer version is available
   
 (2)docker run -d --name=grid -p 4444:24444 -p 5900:25900 \
     -e TZ="US/Pacific" -v /dev/shm:/dev/shm --privileged elgalu/selenium
 (3) 验证:
   docker exec grid wait_all_done 30s
 访问:
 http://192.168.163.130:4444/grid/console
 
 (4)执行
 docker exec -it grid python /dev/shm/app.py 

问题:
 Selenium 'Chrome failed to start: exited abnormally' error
 from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=options)


app.py脚本:
from selenium import webdriver
from time import sleep
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--disable-extensions')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=options)

driver.get('https://www.baidu.com')
print("get baidu")

driver.find_element_by_id("kw").send_keys("docker selenium")
driver.find_element_by_id("su").click()

sleep(1)

driver.get_screenshot_as_file("/root/temp/baidu_img.png")

driver.quit()
print("end...")

 

 

参考:

中文官网
http://www.selenium.org.cn/category/install
https://selenium-python.readthedocs.io/getting-started.html


使用docker做UI自动化测试并在Jenkins运行
https://testerhome.com/topics/16605
https://github.com/elgalu/docker-selenium

你可能感兴趣的:(docker + selenium实操)