DeprecationWarning:在Python中使用Geckodriver和Selenium,将setter用于无头属性,而不是set_headless opts.set_headless(headless = True) [英] 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文件只是说:
I have a very basic Python script that runs perfectly on my local machine (Mint 19), and yet fails on a remote box (Ubuntu 16.04). Same files, both Python 3.7. I have geckodriver in /usr/local/bin and it checks out from path with gecko --version from the command line. I can't figure out what the problem is. The geckodriver.log file just says:
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代码:
Here is the Python code:
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,但由于它是本地到本地连接,因此我不确定这是否重要.
I have port 4444 allowed in the firewall on the remote box, but as it's a local-to-local connection I am not sure that should even matter.
推荐答案
此信息日志...
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 来实现无头属性,如下所示:>
...implies that the set_headless opts.set_headless(headless=True)
is deprecated and you have to use the setter for headless property as follows:
opts = Options()
opts.headless = True
driver = webdriver.Firefox(options=opts)
driver.get("https://www.krypterro.com")
您可以在中找到详细的讨论用python在Selenium中以编程方式使firefox变得无头?
在尝试检索页面源和 Web应用程序已启用 JavaScript ,您需要引入 WebDriverWait ,并且可以使用以下解决方案:
Moving ahead as you are trying to retrive the Page Source and as the Web Application is JavaScript enabled you need to induce WebDriverWait and you can use the following solution:
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()
,而总是仅在tearDown(){}
方法内调用driver.quit()
即可关闭&优雅地销毁 WebDriver 和 Web Client 实例.
Note B: You don't need to invoke driver.close()
and driver.quit()
rather always invoke driver.quit()
only within tearDown(){}
method to close & destroy the WebDriver and Web Client instances gracefully.
这篇关于DeprecationWarning:在Python中使用Geckodriver和Selenium,将setter用于无头属性,而不是set_headless opts.set_headless(headless = True)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!