centos上使用Selenium
CentOS + Selenium 用法小结
安装python pip
安装selenium
pip install selenium
安装chrome-browser
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate
sudo yum install google-chrome-stable_current_x86_64.rpm
安装chromedriver:一个用来和chrome交互的接口
sudo yum install chromedriver
安装xvfb xvfb是一个可以将屏幕的图像输出给放到虚拟内存中的东西
sudo yum update
sudo yum install Xvfb
# vi test.py
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--no-sandbox')
driver = webdriver.Chrome(chrome_options=chrome_options)
for i in range(10):
driver.get("https://www.baidu.com/")
print(driver.title)
driver.close()
运行
xvfb-run python test.py
这个时候大概率会报错
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: A new session could not be created. Details: session not created: This version of ChromeDriver only supports Chrome version xx
这个是因为chromedriver 和chrome-browser版本不 匹配
google-chrome --version
chromedriver
http://npm.taobao.org/mirrors/chromedriver/
下载对应chromedriver
拷到py同目录,py里不要有中文
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
DRIVER_PATH = './chromedriver'
if __name__ == "__main__":
options = Options()
print("==========================selenium Test Start==========================")
options.add_argument('--no-sandbox')
options.add_argument('--headless')
options.add_argument('--disable-gpu')
driver = Chrome(executable_path=DRIVER_PATH, options=options)
driver.get("http://xxxxxxxx6:7xxx/")
print("Title:")
print(driver.title)
driver.close()
driver.quit()
print("==========================selenium Test Done===========================")
运行
xvfb-run python test.py