其目标是使web浏览器自动化,从特定的窗口大小开始。
我尝试过两种实现。使用chromedriver的实现完成了工作,而使用geckodriver的实现失败了。准确地说,这两种实现都启动了浏览器,但其中只有一种设置了指定的窗口大小。我想知道为什么。我是用错了还是有别的东西在手边?
使用geckodriver的实现1号无法设置窗口大小:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
from selenium.webdriver.firefox.service import Service
geckodriver_path = Service('/usr/bin/geckodriver')
firefox_options = Options()
firefox_options.add_argument('--window-size=800,800')
driver = webdriver.Firefox(service = geckodriver_path, options = firefox_options)
driver.get("http://www.wikipedia.org")
print(driver.get_window_size())实现2号,使用chromedriver正确设置窗口大小:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
chromedriver_path = Service('/usr/bin/chromedriver')
chrome_options = Options()
chrome_options.add_argument('--window-size=800,800')
driver = webdriver.Chrome(service = chromedriver_path, options = chrome_options)
driver.get("http://www.wikipedia.org")
print(driver.get_window_size())更多信息:
[boris@E7490-DELL ~]$ python -V
Python 3.10.5
[boris@E7490-DELL ~]$ chromedriver --version
ChromeDriver 103.0.5060.53 (a1711811edd74ff1cf2150f36ffa3b0dae40b17f-refs/branch-heads/5060@{#853})
[boris@E7490-DELL ~]$ geckodriver -V
geckodriver 0.31.0 (b617178ef491 2022-04-06 11:57 +0000)
[boris@E7490-DELL ~]$ google-chrome --version
Google Chrome 103.0.5060.53
[boris@E7490-DELL ~]$ firefox -v
Mozilla Firefox 101.0.1
[boris@E7490-DELL ~]$ python
Python 3.10.5 (main, Jun 9 2022, 00:00:00) [GCC 12.1.1 20220507 (Red Hat 12.1.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import selenium
>>> selenium.__version__
'4.1.0'发布于 2022-06-28 11:39:40
Firefox需要将with和高度维度分开。
firefox_options.add_argument('--width=800')
firefox_options.add_argument('--height=800')https://stackoverflow.com/questions/72784758
复制相似问题