Chapter4 Working with Selenium API
4.1 Checking an element's presence
from selenium.common.exceptions import NoSuchElementException
def isElementPresent(Xpath):
try:
driver.find_element_by_xpath(Xpath)
return True
except NoSuchElementException as e:
print("except:",e)
return False
# global, because it's used in the function
global driver
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
tc = unittest.TestCase()
if isElementPresent("//select"):
area = driver.find_element_by_xpath("//select")
print(area.get_attribute("class"))
else:
tc.fail("element doesn't exist!!")
4.2 Checking an element's state
tc = unittest.TestCase()
button = driver.find_element_by_id("web-nav-app-download-btn")
if button.is_enabled():
button.click()
else:
tc.fail("button not enabled!")
4.3 Using Advanced User Interactions API for mouse and keyboard events
actions = ActionChains(driver)
langList = driver.find_element_by_tag_name("select")
langs = driver.find_elements_by_tag_name("option")
langList.click()
actions.click(langs[0]).key_down(Keys.CONTROL).click(langs[1]).key_up(Keys.CONTROL).perform()
print(len(Select(langList).all_selected_options))
CTRL
or COMMAND
manually no use on Mac...
4.4 Performing double-click on an element
actions = ActionChains(driver)
actions.double_click(button).perform()
4.5 Performing drag-and-drop operations
actions = ActionChains(driver)
source = driver.find_element_by_class_name("qrcode")
target = driver.find_element_by_id("web-nav-app-download-btn")
actions.drag_and_drop(source,target).perform()
4.6 Working with context menus
actions.context_click(clickMe).move_to_element(WechatShare).click().perform()
different types of Errors o(╥﹏╥)o
4.7 Executing the JavaScript code
print(driver.execute_script("return document.title"))
4.8 Capturing screenshots with Selenium WebDriver
button = driver.find_element_by_id("web-nav-app-download-btn")
button.screenshot('download.png')
4.9 Maximizing the browser window
driver.maximize_window()
4.10 Handling session cookie
Method |
Description |
add_cookie(cookie_dict) |
Adds a cookie to your current session |
get_cookie(name) |
Get a single cookie by name. Returns the cookie if found, None if not. |
get_cookies() |
Returns a set of dictionaries, corresponding to cookies visible in the current session. |
delete_all_cookies() |
Delete all cookies in the scope of the session. |
delete_cookie(name) |
Delete a single cookie with the given name. |
4.11 Working with browser navigation
selenium.webdriver.remote.webdriver
Method |
Description |
back() |
Goes one step backward in the browser history. |
forward() |
Goes one step forward in the browser history. |
refresh() |
Refreshes the current page. |
button = driver.find_element_by_id("web-nav-app-download-btn")
#button.screenshot('download.png')
button.click()
driver.back()
4.12 Working with WebDriver events
selenium.webdriver.support.abstract_event_listener.AbstractEventListener
Event |
Description |
before_navigate_to(url,driver) |
This method is called before the get(url) method is called |
after_navigate_to(url,driver) |
This method is called after the get(url) method is called |
before_navigate_back(driver) |
after_navigate_back(driver) |
before_navigate_forward(driver) |
after_navigate_forward(driver) |
before_find(by,value,driver) |
This method is called before find_element(s)_by_...
|
after_find(by,value,driver) |
This method is called after find_element(s)_by_...
|
before_change_value_of(element,driver) |
This method is called before the clear() or the send_keys() method |
after_change_value_of(element,driver) |
This method is called after the clear() or the send_keys() method |
before_click(element,driver) |
This method is called before the click() method |
after_click(element,driver) |
This method is called after the click() method |
before_execute_script(script,driver) |
This method is called before the execute_script() method |
after_execute_script(script,driver) |
THis method is called after the execute_script() method |
on_exception(exception,driver) |
This method is called whenever an exception would be thrown |
from selenium.webdriver.support.events import EventFiringWebDriver, AbstractEventListener
class MyListener(AbstractEventListener):
def before_navigate_to(self,url,driver):
print("Before navigate to %s" % url)
def after_navigate_to(self,url,driver):
print("After navigate to %s" % url)
driver = webdriver.Firefox(executable_path='/usr/local/bin/geckodriver')
ef_driver = EventFiringWebDriver(driver,MyListener())
ef_driver.get("https://www.jianshu.com")
网友评论