Firefox WebDriver

>class selenium.webdriver.firefox.webdriver.WebDriver(firefox_profile=None, firefox_binary=None, timeout=30, capabilities=None, proxy=None, executable_path=’wires’)

>quit()

退出驱动,关闭所有关联的窗口

>NATIVE_EVENTS_ALLOWED = True

>firefox_profile

Chrome WebDriver

>class selenium.webdriver.chrome.webdriver.WebDriver(executable_path=’chromedriver’, port=0, chrome_options=None, service_args=None, desired_capabilities=None, service_log_path=None)

控制 ChromeDriver 并且允许你驱动浏览器

你需要先下载ChromeDriver的[可执行驱动](http://chromedriver.storage.googleapis.com/index.html)

>create_options()

>launch_app(id)

以特定的id 启动 Chrome

>quit()

关闭浏览器,关闭开始时启动的ChromeDriver

Remote WebDriver

>class selenium.webdriver.remote.webdriver.WebDriver(command_executor=’http://127.0.0.1:4444/wd/hub‘, desired_capabilities=None, browser_profile=None, proxy=None, keep_alive=False)

通过对远程服务器发送命令来控制浏览器。远程的服务器需要运行[这里](https://github.com/SeleniumHQ/selenium/wiki/JsonWireProtocol)定义的WebDriver wire 协议

属性: * session_id - WebDriver控制,浏览器会话产生的一个String ID * capabilities - 返回浏览器会话的可用功能dict,有关远程服务区,请看[https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities](https://github.com/SeleniumHQ/selenium/wiki/DesiredCapabilities) * command_executor - remote_connection.RemoteConnection 对象,用来执行命令 * error_handler - errorhandler.ErrorHandler对象,捕获错误

>add_cookie(cookie_dict) - 给当前会话加cookie

参数: * cookie_dict:dict对象,需要指定键`name`和`value`,可选的键有:path,`domain`,`secure`,`expiry`

用法:

driver.add_cookie({'name':'foo','value':'bar'})
driver.add_cookie({'name':'foo','value':'bar','path':'/'})
driver.add_cookie({'name':'foo','value':'bar','path':'/','secure':True})

>back() - 在浏览历史中回退一步

例:

driver.back()

>close() - 关闭当前窗口

例:

driver.close()

>create_web_element(element_id) - 以指定的元素id创建一个新web元素

>delete_all_cookies() - 删除当前会话的所有cookie

例:

driver.delete_all_cookies()

>delete_cookie(name) - 删除一个指定的cookie

例:

driver.delete_cookie('my_cookie')

>execute(driver_command, params=None)

给 command.CommandExecutor 发送一个要执行的命令

参数: * driver_command:要执行的命令名(字符串) * params:命令的参数dict

返回:命令的JSON返回会加载到一个dict对象

>execute_async_script(script,*args)

在当前的窗口/框架里异步执行Javascript

参数: * script:要执行的js * *args:js的任意合适的参数

例:

driver.execute_async_script('document.title')

(这样也算一个例子?参数呢?)

(find_element_*方法,自行参看[原文档](http://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.remote.webdriver))

>forward() - 浏览历史里前进一步

>get(url) - 用当前的浏览器session加载一个web页面

>get_cookie(name) - 返回一个指定的cookie,不存在返回`None`

>get_cookies() - 返回一组dict,相当于当前会话的可用cookie

>get_log(log_type) - 获取指定类型的日志

例:

driver.get_log('browser')
driver.get_log('driver')
driver.get_log('client')
driver.get_log('server')

>get_screenshot_as_base64() - 获取当前页面的截图的base64编码字符串,当页面嵌入了图片时这个方法很有用。

>get_screenshot_as_file(filename) - 获取当前页面截图,如果有任何 IOError则返回False,正常返回True,文件名记得使用完整的路径

例:

driver.get_screenshot_as_file('/Screenshots/foo.png/')

>get_screenshot_as_png - 获取当前窗口截图的二进制数据

>get_window_position(windowHandle=’current’) - 获取当前窗口的x,y位置

>get_window_size(windowHandle=’current’) - 获取当前窗口的宽高(width,height)

>implicitly_wait(time_to_wait) - 设置一个隐式的等待时间,等待一个元素被发现或者一个命令的完成。

在每次会话里,这个方法只需要被调用一次。超时后要调用 execute_async_script,请参看 set_script_timeout

例:

driver.implicitly_wait(30)

>maximize_window() - 将webdriver正在使用的窗口最大化

>quit() 退出驱动并关闭所有关联窗口

>refresh() - 刷新当前页面

>set_page_load_timeout(time_to_wait) - 给载入页面设置一个超时时间,在抛出错误之前会等待到加载完成

>set_script_timeout(time_to_wait) - 在一个 execute_async_script 调用期间,设置脚本等待的时间

>set_window_position(x,y,windowHandle=’current’) - 给页面设置x,y位置

>set_window_size(width,height,windowHandle=’current’) - 给页面设置宽高

>start_client() - 在新会话开启之前调用,这个方法可以重载来定制启动行为

>start_session(desired_capabilities, browser_profile=None) - 以期望的性能来创建一个新会话

参数: * browser_name - 要请求的浏览器的名字 * version - 要请求的浏览器版本 * platform - 浏览器平台 * javascript_enabled - 新会话是否支持js * browser_profile - 仅在请求Firefox浏览器时使用,selenium.webdriver.firefox.firefox_profile.FirefoxProfile 对象对象

>stop_client() - 执行一个退出命令时调用。这个方法可以重写,自己定制关闭时的行为

>switch_to_active_element() - 不推荐使用 driver.switch_to.active_element

>switch_to_alert() - 不推荐使用 driver.switch_to.alert

>switch_to_default_content() - 不推荐使用 driver.switch_to.default_content

>switch_to_frame(frame_reference) - 不推荐使用 driver.switch_to.frame

>switch_to_window(window_name) - 不推荐使用 driver.switch_to.window

>application_cache - 返回一个 ApplicationCache 对象 来和 浏览器应用缓存交互

>current_url - 当前页的url

>current_window_handle - 当前窗口的句柄

>desired_capabilities - 返回当前驱动正在使用的 ‘期望性能’

>file_detector

>log_types - 返回可用的日志类型list

>mobile

>name - 返回当前实例的底层浏览器名

>orientation - 获取当前设备的适应情况

>page_source - 获取当前页面的来源

>switch_to

>title - 当前页面的标题

>window_handles - 返回当前会话内所有窗口的资源句柄

页面元素

>class selenium.webdriver.remote.webelement.WebElement(parent,id_,w3c=False)

代表一个DOM元素。通常所有和文档互动的有趣操作都要通过这个接口执行。

所有的方法调用都会做一个 ‘新鲜检查’ 来确认引用的元素仍然可用,这本质上确定了这个元素是否还与DOM相连。如果检测失败,会抛出 StaleEleementReferenceException 异常,并且下面的所有对这个接口的调用都会失败

(拥有find_element_by_*所有方法,请参看[此处](http://selenium-python.readthedocs.org/api.html#module-selenium.webdriver.remote.webelement))

>get_attribute(name) - 返回元素指定的属性

这个方法首先会尝试返回元素指定的属性,如果属性不存在,它会返回和属性名相同的字符串,如果没有属性是这个名字,返回 None 。 被认为是真假的值会返回布尔类型,其他所有的非`None`值都会以字符串的形式返回。属性不存在,返回 None

例:

# Check if the "active" CSS class is applied to en element
is_active = "active" in target_element.get_attribute("class")

>is_displayed() - 元素对用户是否可见

>is_enabled() - 元素是否可用

>is_selected() - 元素是否被选中,可用来检测单选或者复选按钮是否被选中

>screenshot(filename) - 获取当前元素的截图,有IOError会返回 False ,文件名要包含完整路径

>send_keys(*value) - 模拟向元素输入

使用这个方法发送简单的按键时间或者填充表单字段:

form_textfiled = driver.find_element_by_name("username")
form_textfiled.send_keys("admin")

这个方法还可以用来设置文件:

file_input = driver.find_element_by_name('profilePic')
file_input.send_keys('path/to/profilepic.gif')

>submit() - 提交表单

>value_of_css_property(property_name) - CSS属性的值

>id - selenium使用的内部ID

这个主要是内部使用,简单的使用案例是用来做类似于检测两个元素是否关联到相同的元素上,可以用 == 来比较:

::
if element1 == element2:
print(“These 2 are equal”)

>location - 元素在可渲染的画布上的位置

>location_once_scrolled_into_view

这个属性改变不会发出警告,用这个来检查元素在屏幕的位置以方便我们点击它,这个方法可能造成元素滚动到视图里。 返回屏幕左上角的位置,元素不可见返回`None`

>parent - WebDriver实例的内部引用,元素是从哪里发现的

>rect - 元素尺寸和位置的dict

>screenshot_as_base64 - 当前元素截图的base64编码字符串

>screenshot_as_png - 当前元素截图的二进制数据

>size - 元素的尺寸

>tag_name - 元素的标签名

>text - 元素的文本

UI支持

>class selenium.webdriver.support.select.Select(webelement)

>deselect_all() - 清除所有的选中输入。仅当选择项支持多选的时候可用,如果不支持多选会抛出 NotImplementedError

>deselect_by_index(index) - 取消指定索引的选项的选中。这个方法会检查`index`属性,而不仅仅是计数

>deselect_by_value(value) - 取消所有选项的值匹配的选中状态。

意思就是,当给出一个’foo’值时,会取消选中一个类似这样的选项:

<option value="foo">Bar</option>

>deselect_by_visible_text(text) - 取消选中所有文本匹配的选项

给出’Bar’时,匹配:

<option value="foo">Bar</option>

>select_by_index(index)

>select_by_value(value)

>select_by_visible_text(text)

上述三个和取消选中用法一样,操作由取选变成选中

>all_selected_options - 返回这个选择标签的所有选中选项list

>first_selected_option - 选择标签的的第一个选中项(或者一个正常选择框的当前选中项)

>options - 选择标签的所有选项list

>class selenium.webdriver.support.wait.WebDriverWait(driver, timeout, poll_frequency=0.5, ignored_exceptions=None)

>until(method, message=’‘)

调用驱动提供的方法名当参数,直到方法返回值 不是 False

>until_not(method, message=’‘)

调用驱动提供的方法名当参数,直到方法返回值 是 False

颜色支持

>class selenium.webdriver.support.color.Color(red,green,blue,alpha=1)

颜色变换支持类:

from selenium.webdriver.support.color import Color

print(Color.from_string('#00fff33').rgba)
print(Color.from_string('rgb(1,255,3)').hex)
print(Color.from_string('blue').rgba)

>_static_ from_string(_str_)

>hex

>rgb

>rgba

预期条件支持

>_class_ selenium.webdriver.support.expected_conditions.alert_is_present

预期出现一个弹框

>_class_ selenium.webdriver.support.expected_conditions.element_located_selection_state_to_be(_locator, is_selected)

预期定位一个元素并且检查选中状态是否符合预期, locator 是一个(by,path)的tuple, is_selected 是布尔值

>_class_ selenium.webdriver.support.expected_conditions.element_located_to_be_selected(_locator_)

预期一个定位的元素是选中的

>_class_ selenium.webdriver.support.expected_conditions.element_selection_state_to_be(_element,is_selected_)

预期元素是否选中

>_class_ selenium.webdriver.support.expected_conditions.element_to_be_clickable(locator)

预期一个元素是否可见可用,以便可以点击它

>_class_ selenium.webdriver.support.expected_conditions.element_to_be_selected(element)

预期一个元素是选中的

>_class_ selenium.webdriver.support.expected_conditions.frame_to_be_available_and_switch_to_it(locator)

检查框架是否可以被切换,如果可以,那么就切换到这个框架

>_class_ selenium.webdriver.support.expected_conditions.invisibility_of_element_locator(locator)

预期元素不可见或者不在DOM上, locator 定位元素

>_class_ selenium.webdriver.support.expected_conditions.presence_of_all_elements_located(locator)

预期至少有一个元素出现在web页面上, locator 是用来寻找已经被定位了的WebElements list

>_class_ selenium.webdriver.support.expected_conditions.presence_of_element_located(locator)

预期元素正在页面的DOM上,这不意味着元素是可见的。

>_class_ selenium.webdriver.support.expected_conditions.staleness_of(element)

等待元素不再附在DOM上, element 是要等待的元素,如果元素仍然在DOM上返回 False ,否则返回 True

>_class_ selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(_locator, text__)

预期给定的文本会出现在指定的元素上

>_class_ selenium.webdriver.support.expected_conditions.text_to_be_present_in_element_value(_locator, text__)

预期给定的文本会出现在指定的元素的value上

>_class_ selenium.webdriver.support.expected_conditions.title_contains(_title_)

预期标题包含一个指定的字符串(大小写敏感),匹配返回True,否则返回False

>_class_ selenium.webdriver.support.expected_conditions.title_is(_title_)

预期标题完全匹配一个字符串

>_class_ selenium.webdriver.support.expected_conditions.visibility_of(_element_)

预期已经在DOM上的一个元素是可见的。可见不仅仅表示元素是显示的,而且长宽都要大于0.参数_element_是一个 WebElement 。如果元素可见,则返回这个元素对象

>_class_ selenium.webdriver.support.expected_conditions.visibility_of_element_located(_locator_)

和`visibility_of`类似,不同的是通过一个定位器来定位元素。如果元素被定位到并且可见,则返回这个元素对象