gpt4 book ai didi

python-3.x - 在Python中使用Geckodriver和Selenium警告: use setter for headless property instead of set_headless opts. set_headless(headless = True)

转载 作者:行者123 更新时间:2023-12-05 00:11:37 32 4
gpt4 key购买 nike

我有一个非常基本的Python脚本,该脚本可以在本地计算机(Mint 19)上完美运行,但是在远程机器上(Ubuntu 16.04)却无法运行。相同的文件,两个Python 3.7。我在/usr/local/bin中有geckodriver,它从命令行使用gecko --version从路径中 check out 。我不知道是什么问题。 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) 已被 弃用,并且您必须将 设置程序用于 headless 属性,如下所示:
opts = Options()
opts.headless = True
driver = webdriver.Firefox(options=opts)
driver.get("https://www.krypterro.com")

您可以在 How to make firefox headless programmatically in Selenium with python?中找到详细的讨论

在尝试检索页面源并且启用了 Web Application的情况下继续前进,您需要引入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(),而是始终仅在 driver.quit()方法内调用 tearDown(){}即可正常关​​闭和销毁WebDriver和Web Client实例。

关于python-3.x - 在Python中使用Geckodriver和Selenium警告: use setter for headless property instead of set_headless opts. set_headless(headless = True),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53134306/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com