资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Python中使用selenium获取浏览器窗口坐标-创新互联

怎么在Python中使用selenium 获取浏览器窗口坐标?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

为萧山等地区用户提供了全套网页设计制作服务,及萧山网站建设行业解决方案。主营业务为成都做网站、网站制作、成都外贸网站建设、萧山网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

1.0 获取浏览器窗口坐标

python目录可找到Webdriver.py 文件定义了get_window_rect()函数,可获取窗口的坐标和大小(长宽),但出现”Command not found”的情况。set_window_rect()函数也一样。

def get_window_rect(self):
 """
 Gets the x, y coordinates of the window as well as height and width of
 the current window.

 :Usage:
  driver.get_window_rect()
 """
 return self.execute(Command.GET_WINDOW_RECT)['value']

def set_window_rect(self, x=None, y=None, width=None, height=None):
 """
 Sets the x, y coordinates of the window as well as height and width of
 the current window.

 :Usage:
  driver.set_window_rect(x=10, y=10)
  driver.set_window_rect(width=100, height=200)
  driver.set_window_rect(x=10, y=10, width=100, height=200)
 """
 if (x is None and y is None) and (height is None and width is None):
  raise InvalidArgumentException("x and y or height and width need values")

 return self.execute(Command.SET_WINDOW_RECT, 
  {"x": x, "y": y, "width": width, "height": height})['value']

然而Webdriver.py文件还定义了get_window_position()函数和get_window_size()函数,可以用这两个函数来分别获取窗口的坐标和大小,而不需要用到win32gui的方法。

def get_window_size(self, windowHandle='current'):
  """
  Gets the width and height of the current window.

  :Usage:
   driver.get_window_size()
  """
  command = Command.GET_WINDOW_SIZE
  if self.w3c:
   if windowHandle != 'current':
    warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
   size = self.get_window_rect()
  else:
   size = self.execute(command, {'windowHandle': windowHandle})

  if size.get('value', None) is not None:
   size = size['value']

  return {k: size[k] for k in ('width', 'height')}
def get_window_position(self, windowHandle='current'):
  """
  Gets the x,y position of the current window.

  :Usage:
   driver.get_window_position()
  """
  if self.w3c:
   if windowHandle != 'current':
    warnings.warn("Only 'current' window is supported for W3C compatibile browsers.")
   position = self.get_window_rect()
  else:
   position = self.execute(Command.GET_WINDOW_POSITION,
         {'windowHandle': windowHandle})['value']

  return {k: position[k] for k in ('x', 'y')}

2.0 获取窗口句柄

handle = driver.current_window_handle #获取当前窗口句柄
handles = driver.window_handles #获取所有窗口句柄

切换句柄可以使用

dr.switch_to.window(handle) #其中handle为获取到的窗口句柄

假设handles为获取到的所有窗口,则handles为一个list,可使用访问list的方法读取句柄。

dr.switch_to.windows(handles[0]) #切换到第一个窗口的句柄
dr.switch_to.windows(handles[-1]) #切换到最新窗口的句柄
python有哪些常用库

python常用的库:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。

看完上述内容,你们掌握怎么在Python中使用selenium 获取浏览器窗口坐标的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


新闻名称:怎么在Python中使用selenium获取浏览器窗口坐标-创新互联
网站URL:http://cdkjz.cn/article/pjopi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220