1.下载并安装
创建目录 mkdir firefox
下载Firefox 在Linux的安装包 https://ftp.mozilla.org/pub/firefox/releases/
wget https://ftp.mozilla.org/pub/firefox/releases/98.0b9/linux-x86_64/zh-CN/firefox-98.0b9.tar.bz2 下载 bzip2 yum install bzip2 解压 tar xjvf firefox-98.0b9.tar.bz2 ln -s /root/firefox/firefox/firefox /usr/bin/firefox
下载Firefox driver https://github.com/mozilla/geckodriver/releases
wget https://github.91chi.fun/https://github.com//mozilla/geckodriver/releases/download/v0.30.0/geckodriver-v0.30.0-linux64.tar.gz 解压 tar -zxvf geckodriver-v0.30.0-linux64.tar.gz ln -s /root/firefox/geckodriver /usr/bin/geckodriver
2.执行Python
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.options import Options
firefox_options = Options()
firefox_options.add_argument('--headless')
driver = webdriver.Firefox(executable_path="/usr/bin/geckodriver", options=firefox_options)
driver.get('https://www.baidu.com')
print(driver.title)
driver.find_element(By.ID, "kw").send_keys("Selenium")
driver.find_element(By.ID, "su").click()
time.sleep(1)
driver.get_screenshot_as_file("./100.png")
time.sleep(1)
driver.quit()
|