源码
def current_window_handle(self):
"""
Returns the handle of the current window.
:Usage:
driver.current_window_handle
"""
if self.w3c:
return self.execute(Command.W3C_GET_CURRENT_WINDOW_HANDLE)['value']
else:
return self.execute(Command.GET_CURRENT_WINDOW_HANDLE)['value']
@property
def window_handles(self):
"""
Returns the handles of all windows within the current session.
:Usage:
driver.window_handles
"""
if self.w3c:
return self.execute(Command.W3C_GET_WINDOW_HANDLES)['value']
else:
return self.execute(Command.GET_WINDOW_HANDLES)['value']
解释: current_window_handle()
返回当前窗口句柄,window_handles()
返回当前会话中所有窗口的句柄。
了解 switch_to()
def switch_to(self):
"""
:Returns:
- SwitchTo: an object containing all options to switch focus into
:Usage:
element = driver.switch_to.active_element
alert = driver.switch_to.alert
driver.switch_to.default_content()
driver.switch_to.frame('frame_name')
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
driver.switch_to.parent_frame()
driver.switch_to.window('main')
"""
return self._switch_to
- driver.switch_to.active_element
def active_element(self):
"""
Returns the element with focus, or BODY if nothing has focus.
:Usage:
element = driver.switch_to.active_element
"""
if self._driver.w3c:
return self._driver.execute(Command.W3C_GET_ACTIVE_ELEMENT)['value']
else:
return self._driver.execute(Command.GET_ACTIVE_ELEMENT)['value']
说明: 返回带有焦点的元素,如果没有焦点则返回BODY。
- alert = driver.switch_to.alert
def alert(self):
"""
Switches focus to an alert on the page.
:Usage:
alert = driver.switch_to.alert
"""
alert = Alert(self._driver)
alert.text
return alert
说明: 将焦点切换到页面上的警报。
- driver.switch_to.default_content()
def default_content(self):
"""
Switch focus to the default frame.
:Usage:
driver.switch_to.default_content()
"""
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': None})
说明: 切换到默认的frame。
def frame(self, frame_reference):
"""
Switches focus to the specified frame, by index, name, or webelement.
:Args:
- frame_reference: The name of the window to switch to, an integer representing the index, or a webelement that is an (i)frame to switch to.
:Usage:
driver.switch_to.frame('frame_name')
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
"""
if isinstance(frame_reference, basestring) and self._driver.w3c:
try:
frame_reference = self._driver.find_element(By.ID, frame_reference)
except NoSuchElementException:
try:
frame_reference = self._driver.find_element(By.NAME, frame_reference)
except NoSuchElementException:
raise NoSuchFrameException(frame_reference)
self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
说明: 通过索引、名称或Web标签将焦点切换到指定的frame。
官方用例:
driver.switch_to.frame(‘frame_name’)
driver.switch_to.frame(1)
driver.switch_to.frame(driver.find_elements_by_tag_name(“iframe”)[0])
- driver.switch_to.parent_frame()
def parent_frame(self):
"""
Switches focus to the parent context. If the current context is the top level browsing context, the context remains unchanged.
:Usage:
driver.switch_to.parent_frame()
"""
self._driver.execute(Command.SWITCH_TO_PARENT_FRAME)
说明: 将焦点切换到父frame。如果当前内容到达浏览器顶层,则保持不变。
- driver.switch_to.window(‘main’)
def window(self, window_name):
"""
Switches focus to the specified window.
:Args:
- window_name: The name or window handle of the window to switch to.
:Usage:
driver.switch_to.window('main')
"""
if self._driver.w3c:
self._w3c_window(window_name)
return
data = {'name': window_name}
self._driver.execute(Command.SWITCH_TO_WINDOW, data)
说明: 将焦点切换到指定的窗口。
selenium的基本操作
from selenium import webdriver
import time
Broswer = webdriver.Chrome(executable_path="chromedriver", port=0,
options=None, service_args=None,
desired_capabilities=None,
chrome_options=None, keep_alive=True)
Broswer.get(url='https://www.so.com/?src=so.com')
print("此实例的基础浏览器的名称:",Broswer.name)#返回此实例的基础浏览器的名称。
print('the title of the current page:',Broswer.title)#Returns the title of the current page.
"""
此实例的基础浏览器的名称: chrome
the title of the current page: 360搜索,SO靠谱
"""
print('url:',Broswer.current_url)
"""
url: https://www.so.com/?src=so.com
"""
print('返回页面源码')
print(Broswer.page_source)
"""
<html class="w3c"><!--<![endif]--><head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>360搜索,SO靠谱</title>
....
"""
Broswer.refresh()#刷新页面
Broswer.maximize_window()#最大化页面
time.sleep(1)
Broswer.minimize_window()#最小化页面
print('获取cookies')
print(Broswer.get_cookies())
time.sleep(1)
Broswer.set_window_size(800,600)
print('当前窗口大小:',Broswer.get_window_size())
"""
当前窗口大小: {'width': 802, 'height': 602}
"""
time.sleep(1)
Broswer.set_window_position(0,0)#设置窗口位置
print('当前窗口位置:',Broswer.get_window_position())
"""
当前窗口位置:0.0
"""
time.sleep(1)
Broswer.set_window_rect(x=10, y=10, width=100, height=200)#设置窗口的x、y坐标以及当前窗口的高度和宽度。
print('当前窗口位置:',Broswer.get_window_rect())#获取窗口的x、y坐标以及当前窗口的高度和宽度。
"""
当前窗口位置: {'height': 202, 'width': 515, 'x': 9, 'y': 9}
"""
print('关闭页面')
Broswer.close()
print('退出浏览器')
Broswer.quit()
来源:CSDN
作者:傻子丶疯子
链接:https://blog.csdn.net/qq_43577241/article/details/104477357