首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用selenium.webdriver.firefox.options?

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

Stack Overflow用户
提问于 2022-06-28 10:25:15
回答 1查看 2.4K关注 0票数 -1

其目标是使web浏览器自动化,从特定的窗口大小开始。

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

使用geckodriver的实现1号无法设置窗口大小:

代码语言:javascript
复制
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正确设置窗口大小:

代码语言:javascript
复制
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())

更多信息:

代码语言:javascript
复制
[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'
EN

回答 1

Stack Overflow用户

发布于 2022-06-28 11:39:40

Firefox需要将with和高度维度分开。

代码语言:javascript
复制
firefox_options.add_argument('--width=800')
firefox_options.add_argument('--height=800')
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72784758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档