deffind_element_by_id(self, id_):"""Finds an element by id.:Args:- id\_ - The id of the element to be found.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_id('foo')"""return self.find_element(by=By.ID, value=id_)deffind_elements_by_id(self, id_):"""Finds multiple elements by id.:Args:- id\_ - The id of the elements to be found.:Returns:- list of WebElement - a list with elements if any was found. Anempty list if not:Usage:elements = driver.find_elements_by_id('foo')"""return self.find_elements(by=By.ID, value=id_)deffind_element_by_xpath(self, xpath):"""Finds an element by xpath.:Args:- xpath - The xpath locator of the element to find.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_xpath('//div/td[1]')"""return self.find_element(by=By.XPATH, value=xpath)deffind_elements_by_xpath(self, xpath):"""Finds multiple elements by xpath.:Args:- xpath - The xpath locator of the elements to be found.:Returns:- list of WebElement - a list with elements if any was found. Anempty list if not:Usage:elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]")"""return self.find_elements(by=By.XPATH, value=xpath)deffind_element_by_link_text(self, link_text):"""Finds an element by link text.:Args:- link_text: The text of the element to be found.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_link_text('Sign In')"""return self.find_element(by=By.LINK_TEXT, value=link_text)deffind_elements_by_link_text(self, text):"""Finds elements by link text.:Args:- link_text: The text of the elements to be found.:Returns:- list of webelement - a list with elements if any was found. anempty list if not:Usage:elements = driver.find_elements_by_link_text('Sign In')"""return self.find_elements(by=By.LINK_TEXT, value=text)deffind_element_by_partial_link_text(self, link_text):"""Finds an element by a partial match of its link text.:Args:- link_text: The text of the element to partially match on.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_partial_link_text('Sign')"""return self.find_element(by=By.PARTIAL_LINK_TEXT, value=link_text)deffind_elements_by_partial_link_text(self, link_text):"""Finds elements by a partial match of their link text.:Args:- link_text: The text of the element to partial match on.:Returns:- list of webelement - a list with elements if any was found. anempty list if not:Usage:elements = driver.find_elements_by_partial_link_text('Sign')"""return self.find_elements(by=By.PARTIAL_LINK_TEXT, value=link_text)deffind_element_by_name(self, name):"""Finds an element by name.:Args:- name: The name of the element to find.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_name('foo')"""return self.find_element(by=By.NAME, value=name)deffind_elements_by_name(self, name):"""Finds elements by name.:Args:- name: The name of the elements to find.:Returns:- list of webelement - a list with elements if any was found. anempty list if not:Usage:elements = driver.find_elements_by_name('foo')"""return self.find_elements(by=By.NAME, value=name)deffind_element_by_tag_name(self, name):"""Finds an element by tag name.:Args:- name - name of html tag (eg: h1, a, span):Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_tag_name('h1')"""return self.find_element(by=By.TAG_NAME, value=name)deffind_elements_by_tag_name(self, name):"""Finds elements by tag name.:Args:- name - name of html tag (eg: h1, a, span):Returns:- list of WebElement - a list with elements if any was found. Anempty list if not:Usage:elements = driver.find_elements_by_tag_name('h1')"""return self.find_elements(by=By.TAG_NAME, value=name)deffind_element_by_class_name(self, name):"""Finds an element by class name.:Args:- name: The class name of the element to find.:Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_class_name('foo')"""return self.find_element(by=By.CLASS_NAME, value=name)deffind_elements_by_class_name(self, name):"""Finds elements by class name.:Args:- name: The class name of the elements to find.:Returns:- list of WebElement - a list with elements if any was found. Anempty list if not:Usage:elements = driver.find_elements_by_class_name('foo')"""return self.find_elements(by=By.CLASS_NAME, value=name)deffind_element_by_css_selector(self, css_selector):"""Finds an element by css selector.:Args:- css_selector - CSS selector string, ex: 'a.nav#home':Returns:- WebElement - the element if it was found:Raises:- NoSuchElementException - if the element wasn't found:Usage:element = driver.find_element_by_css_selector('#foo')"""return self.find_element(by=By.CSS_SELECTOR, value=css_selector)deffind_elements_by_css_selector(self, css_selector):"""Finds elements by css selector.:Args:- css_selector - CSS selector string, ex: 'a.nav#home':Returns:- list of WebElement - a list with elements if any was found. Anempty list if not:Usage:elements = driver.find_elements_by_css_selector('.foo')"""return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)deffind_element(self, by=By.ID, value=None):"""Find an element given a By strategy and locator. Prefer the find_element_by_* methods whenpossible.:Usage:element = driver.find_element(By.ID, 'foo'):rtype: WebElement"""if self.w3c:if by == By.ID:by = By.CSS_SELECTORvalue ='[id="%s"]'% valueelif by == By.TAG_NAME:by = By.CSS_SELECTORelif by == By.CLASS_NAME:by = By.CSS_SELECTORvalue =".%s"% valueelif by == By.NAME:by = By.CSS_SELECTORvalue ='[name="%s"]'% valuereturn self.execute(Command.FIND_ELEMENT,{'using': by,'value': value})['value']deffind_elements(self, by=By.ID, value=None):"""Find elements given a By strategy and locator. Prefer the find_elements_by_* methods whenpossible.:Usage:elements = driver.find_elements(By.CLASS_NAME, 'foo'):rtype: list of WebElement"""if self.w3c:if by == By.ID:by = By.CSS_SELECTORvalue ='[id="%s"]'% valueelif by == By.TAG_NAME:by = By.CSS_SELECTORelif by == By.CLASS_NAME:by = By.CSS_SELECTORvalue =".%s"% valueelif by == By.NAME:by = By.CSS_SELECTORvalue ='[name="%s"]'% value# Return empty list if driver returns null# See https://github.com/SeleniumHQ/selenium/issues/4555return self.execute(Command.FIND_ELEMENTS,{'using': by,'value': value})['value']or[]
deffind_element(self,=By.ID, value=None):"""Find an element given a By strategy and locator. Prefer the find_element_by_* methods whenpossible.:Usage:element = driver.find_element(By.ID, 'foo'):rtype: WebElement"""if self.w3c:if by == By.ID:by = By.CSS_SELECTORvalue ='[id="%s"]'% valueelif by == By.TAG_NAME:by = By.CSS_SELECTORelif by == By.CLASS_NAME:by = By.CSS_SELECTORvalue =".%s"% valueelif by == By.NAME:by = By.CSS_SEhj0ECTORvalue ='[name="%s"]'% valuereturn self.execute(Command.FIND_ELEMENT,{'using': by,'value': value})['value']
defsend_keys(self,*value):"""Simulates typing into the element.:Args:- value - A string for typing, or setting form fields. For settingfile inputs, this could be a local file path.Use this to send simple key events or to fill out form fields::form_textfield = driver.find_element_by_name('username')form_textfield.send_keys("admin")This can also be used to set file inputs.::file_input = driver.find_element_by_name('profilePic')file_input.send_keys("path/to/profilepic.gif")# Generally it's better to wrap the file path in one of the methods# in os.path to return the actual path to support cross OS testing.# file_input.send_keys(os.path.abspath("path/to/profilepic.gif"))"""# transfer file to another machine only if remote driver is used# the same behaviour as for java bindingif self.parent._is_remote:local_file = self.parent.file_detector.is_local_file(*value)if local_file isnotNone:value = self._upload(local_file)self._execute(Command.SEND_KEYS_TO_ELEMENT,{'text':"".join(keys_to_typing(value)),'value': keys_to_typing(value)})# Private Methodsdef_execute(self, command, params=None):"""Executes a command against the underlying HTML element.Args:command: The name of the command to _execute as a string.params: A dictionary of named parameters to send with the command.Returns:The command's JSON response loaded into a dictionary object."""ifnot params:params ={}params['id']= self._idreturn self._parent.execute(command, params)