如何使用selenium.webdriver.firefox.options?



目标是使web浏览器自动以特定的窗口大小启动。

我已经尝试了两种实现。使用chromedriver的实现完成了工作,而使用geckodriver的实现失败了。更精确地说,启动浏览器都实现,但只有其中一个集指定的窗口大小。我想知道为什么。是我用错了,还是手边有别的东西?

没有实现。1、用geckodriver设置窗口大小失败:

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'

Firefox需要分隔宽度和高度。

firefox_options.add_argument('--width=800')
firefox_options.add_argument('--height=800')

最新更新