python selenium 在linux平台下获取页面为空

selenium linux 平台获取页面为空

在linux下获取的网页源码为一下内容

<html xmlns="http://www.w3.org/1999/xhtml"><head>head><body>body>html>

解决方案

  • 方案一
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800, 600))
display.start()
# Do Not use headless chrome option
# options.add_argument('headless')
url = 'https://10.11.227.21/tmui/'
driver.get(url + "login.jsp")
html_source = driver.page_source
print(html_source)

#xvfb is required to use "pyvortualdisplay"
#$ sudo apt-get install -y xvfb
  • 方案二
from selenium import webdriver
from selenium.webdriver import DesiredCapabilities
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--headless")

capabilities = DesiredCapabilities.CHROME.copy()
capabilities['acceptSslCerts'] = True
capabilities['acceptInsecureCerts'] = True

driver = webdriver.Chrome(chrome_options = chrome_options,executable_path='your path',desired_capabilities=capabilities)
driver.get("yourWebsite")

你可能感兴趣的:(python)