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

2023-11-27

我有一个非常基本的 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) is 已弃用你必须使用setter对于无头属性如下:

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

您可以在中找到详细的讨论如何使用 python 在 Selenium 中以编程方式使 Firefox 无头?

当您尝试检索时继续前进页面来源并作为Web应用程序 is JavaScript启用你需要诱导WebDriver等待您可以使用以下解决方案:

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()

Note B: 你不需要调用driver.close() and driver.quit()而总是调用driver.quit()仅在之内tearDown(){}方法关闭并销毁网络驱动程序 and 网页客户端优雅地实例。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

DeprecationWarning:在 Python 中使用 Geckodriver 和 Selenium 对无头属性使用 setter,而不是 set_headless opts.set_headless(headless=True) 的相关文章

随机推荐