示例代码:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import time
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument("user-data-dir=selenium")
print("Opening browser")
driver = webdriver.Chrome("/usr/lib/chromium-browser/chromedriver", options=chrome_options)
print("getting request")
driver.get("http://www.tsetmc.com/Loader.aspx?ParTree=15131F")
print("starting wait")
time.sleep(10)
response = driver.page_source
print("got response, quitting...")
driver.quit()
报错信息:
Fail To Create Data Directory
Google Chrome cannot read and write to its data directory : selenium
解决方法:
修改chrome_options.add_argument("user-data-dir=selenium")
成如下代码:
import pathlib
scriptDirectory = pathlib.Path().absolute()
options.add_argument(f"user-data-dir={scriptDirectory}\\userdata")#实际脚本的目录下的userdata
或者
options.add_argument("user-data-dir=C:\environments\selenium")