我正在寻找在selenium中设置窗口大小。我找到了这个解决方案How to set window size in Selenium Chrome Python
rom selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("--headless")
options.add_argument("window-size=1400,600")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe', service_args=["--log-path=./Logs/DubiousDan.log"])
driver.get("http://google.com/")
print ("Headless Chrome Initialized")
print(driver.get_window_size())
driver.set_window_size(1920, 1080)
size = driver.get_window_size()
print("Window size: width = {}px, height = {}px".format(size["width"], size["height"]))
driver.quit()
有什么区别吗?
options.add_argument("window-size=1400,600")
或做
driver.set_window_size(1920, 1080)
我注释掉了windows机器中的无头选项,并检查了代码。使用driver.set_window_size(1920, 1080)
,我可以在浏览器上看到窗口大小的变化。但是对于options.add_argument("window-size=1920, 1080")
,我看不出有什么变化,options.add_argument("window-size=1920, 1080")
只是用于无头模式吗?
发布于 2022-03-16 11:58:52
将驱动程序窗口大小设置为
driver.set_window_size(1920, 1080)
并通过使用Options
来实现这一点
options.add_argument("window-size=1920,1080")
似乎是一样的。
就像是
driver.get(url)
和
driver.navigate().to(url)
WebDriver方法正在执行exactly the same thing,实际上是同义词。
我能注意到的唯一区别是将驱动程序窗口大小设置为
driver.set_window_size(1920, 1080)
对options.add_argument("window-size=1920,1080")
是否将驱动程序窗口大小设置/更改为
driver.set_window_size(1920, 1080)
可以在代码中的任何地方执行,甚至几次,同时将驱动程序窗口大小设置为
options.add_argument("window-size=1920,1080")
只能执行一次,然后才能使用
driver = webdriver.Chrome(chrome_options=options, executable_path=the_path
)
发布于 2022-03-16 12:16:32
如果您查看源代码:
set_window_size
代码:
def set_window_size(self, width, height, windowHandle='current'):
"""
Sets the width and height of the current window. (window.resizeTo)
:Args:
- width: the width in pixels to set the window to
- height: the height in pixels to set the window to
:Usage:
driver.set_window_size(800,600)
"""
if self.w3c:
if windowHandle != 'current':
warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
self.set_window_rect(width=int(width), height=int(height))
else:
self.execute(Command.SET_WINDOW_SIZE, {
'width': int(width),
'height': int(height),
'windowHandle': windowHandle})
它被明确提到,它设置了
当前窗口的
宽度和高度
需要两个args:
- width: the width in pixels to set the window to
- height: the height in pixels to set the window to
add_argument
代码:
def add_argument(self, argument):
"""
Adds an argument to the list
:Args:
- Sets the arguments
"""
if argument:
self._arguments.append(argument)
else:
raise ValueError("argument can not be null")
现在来问你的问题:
,我注释掉了windows机器中的无头选项,并检查了代码。使用driver.set_window_size(1920,1080),我可以在浏览器上看到窗口大小的变化。但是对于options.add_argument("window-size=1920,1080"),我不认为options.add_argument(“窗口大小=1920,1080")只适用于无头模式吗?
使用driver.set_window_size(1920, 1080)
,我可以在浏览器上看到窗口大小的变化--这是意料之中的。
options.add_argument("window-size=1920, 1080")
,我看不出有什么变化是options.add_argument("window-size=1920, 1080")
只用于无头模式?-不,理想情况下它应该在1920年和1080像素启动。add_argument
只是将一个arg
添加到列表中。执行options.add_argument("--headless")
是可选的。
https://stackoverflow.com/questions/71496360
复制相似问题