DeprecationWarning:使用setter for headless 属性 而不是set_headless opts.set_headless(headless=True) 在Python 中使用Geckodriver 和Selenium

DeprecationWarning: use setter for headless property instead of set_headless opts.set_headless(headless=True) using Geckodriver and Selenium in Python

我有一个非常基本的 Python 脚本,可以在我的本地机器 (Mint 19) 上完美运行,但在远程机器 (Ubuntu 16.04) 上运行失败。相同的文件,都是 Python 3.7。我在 /usr/local/bin 中有 geckodriver,它在命令行中使用 gecko --version 从路径中检出。我不知道是什么问题。 geckodriver.log 文件只是说:

1541268536111   mozrunner::runner   INFO    Running command: "/usr/bin/firefox" "-marionette" "-headless" "-foreground" "-no-remote" "-profile" "/tmp/rust_mozprofile.Mt6zAyZc7D01"
*** You are running in headless mode.
1541268546125   Marionette  INFO    Listening on port 33632

来自终端的错误是:

root@dev1:/home/krypterro/PycharmProjects/corbot# python3 test1.py
 2018-11-03 12:28:22,442 -  INFO -  Application - Start
test1.py:12: DeprecationWarning: use setter for headless property instead of set_headless
  opts.set_headless(headless=True)
Traceback (most recent call last):
  File "test1.py", line 21, in <module>
    main()
  File "test1.py", line 14, in main
    driver = webdriver.Firefox(options=opts)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/firefox/webdriver.py", line 174, in __init__
    keep_alive=True)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 157, in __init__
    self.start_session(capabilities, browser_profile)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 252, in start_session
    response = self.execute(Command.NEW_SESSION, parameters)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/usr/local/lib/python3.7/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: timed out

这里是 Python 代码:

from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import logging

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


def main():
    logging.info('Application - Start')
    # Operate in headless mode
    opts = Options()
    opts.set_headless(headless=True)
    assert opts.headless
    driver = webdriver.Firefox(options=opts)
    driver.get("https://www.krypterro.com")
    html_src = driver.page_source
    print(html_src)
    driver.close()
    driver.quit()
    logging.info('Application - End')
main()

我在远程机器的防火墙中允许端口 4444,但由于它是本地到本地的连接,我不确定这是否重要。

此信息日志...

INFO - Application - Start test1.py:12: DeprecationWarning: use setter for headless property instead of set_headless opts.set_headless(headless=True)

...表示 set_headless opts.set_headless(headless=True) 弃用 并且您必须使用 setter for headless 属性 如下:

opts = Options()
opts.headless = True
driver = webdriver.Firefox(options=opts)
driver.get("https://www.krypterro.com")

您可以在

中找到详细的讨论

继续前进,因为您正在尝试检索 Page Source,并且当 Web Application is JavaScript 启用时,您需要引入 WebDriverWait您可以使用以下解决方案:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

driver.get("https://www.krypterro.com")
WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h2[contains(.,'Products and Services')]")))
    html_src = driver.page_source
    print(html_src)
    driver.quit()

注意 B:您不需要调用 driver.close()driver.quit(),而总是仅在 [=17= 内调用 driver.quit() ] 方法来优雅地关闭和销毁 WebDriverWeb Client 实例。