openwpm.commands.utils.webdriver_utils module¶
- openwpm.commands.utils.webdriver_utils.click_to_element(element, sleep_after=0.5)[source]¶
Click to element and handle WebDriverException.
- openwpm.commands.utils.webdriver_utils.execute_in_all_frames(driver, func, kwargs={}, frame_stack=['default'], max_depth=5, logger=None, visit_id=-1)[source]¶
Recursively apply func within each iframe
When called at each level, func will be passed the webdriver instance as an argument as well as any named arguments given in kwargs. If you require a return value from func it should be stored in a mutable argument. Function returns and positional arguments are not supported. func should be defined with the following structure:
>>> def print_and_gather_links(driver, frame_stack, >>> print_prefix='', links=[]): >>> elems = driver.find_elements(By.TAG_NAME, 'a') >>> for elem in elems: >>> link = elem.get_attribute('href') >>> print print_prefix + link >>> links.append(link)
execute_in_all_frames should then be called as follows:
>>> all_links = list() >>> execute_in_all_frames(driver, print_and_gather_links, >>> {'prefix': 'Link ', 'links': all_links}) >>> print "All links on page (including all iframes):" >>> print all_links
- Parameters:
driver (selenium.webdriver) – A Selenium webdriver instance.
func (function) – A function handle to apply to the webdriver instance within each frame
max_depth (int) – Maximum depth to recurse into
frame_stack (list of selenium.webdriver.remote.webelement.WebElement) – list of parent frame handles (including current frame)
logger (logger) – logging module’s logger
visit_id (int) – ID of the visit
- openwpm.commands.utils.webdriver_utils.execute_script_with_retry(driver, script)[source]¶
Execute script, retrying if a WebDriverException is thrown
See: https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/7931#issuecomment-192191013
- openwpm.commands.utils.webdriver_utils.get_button_text(element)[source]¶
Get the text either via value attribute or using (inner) text.
value attribute works for <input type=”button”…> or <input type=”submit”.
text works for <button>elements, e.g. <button>text</button>.
- openwpm.commands.utils.webdriver_utils.is_active(input_element)[source]¶
Check if we can interact with the given element.
- openwpm.commands.utils.webdriver_utils.is_clickable(driver, full_xpath, xpath, timeout=1)[source]¶
Check if an element is visible and enabled.
Selenium requires an element to be visible and enabled to be clickable. We extend that to require it to have a tag capable of containing a link. NOTE: doesn’t work 100%
- openwpm.commands.utils.webdriver_utils.is_visible(driver, locator_type, locator, timeout=3)[source]¶
- openwpm.commands.utils.webdriver_utils.move_to_and_click(driver, element, sleep_after=0.5)[source]¶
Scroll to the element, hover over it, and click it
- openwpm.commands.utils.webdriver_utils.parse_neterror(error_message)[source]¶
Attempt to parse the about:neterror message.
If any errors occur while parsing, we fall back to the unparsed message
- openwpm.commands.utils.webdriver_utils.switch_to_parent_frame(driver, frame_stack)[source]¶
Switch driver to parent frame
Selenium doesn’t provide a method to switch up to a parent frame. Any frame handles collected in a parent frame can’t be used in the child frame, so the only way to switch to a parent frame is to switch back to the top-level frame and then switch back down to the parent through all iframes.
- Parameters:
driver (selenium.webdriver) – A Selenium webdriver instance.
frame_stack (list of selenium.webdriver.remote.webelement.WebElement) – list of parent frame handles (including current frame)
- openwpm.commands.utils.webdriver_utils.wait_and_find(driver, locator_type, locator, timeout=3, check_iframes=True)[source]¶
Search for element with locator and block if not found
- Parameters:
driver (selenium.webdriver.firefox.webdriver.WebDriver) – An instance of the Firefox webdriver
locator_type (string) – A text representation of the attribute to search by, e.g. searching by id, class name, and so on. For a list of supported types, import selenium.webdriver.common.by.By and use By.LINK_TEXT, By.ID, and so on.
locator (string) – The search string used to identify the candidate element.
timeout (int, optional) – Time in seconds to block before throwing NoSuchElementException. The default is 3 seconds.
check_iframes (bool, optional) – Set to True to also check all iframes contained directly in the current frame.
- Returns:
Matching element (if any is found before timeout).
- Return type:
selenium.webdriver.firefox.webelement.FirefoxWebElement
- Raises:
NoSuchElementException – Raised if no element is located with locator before timeout.