Summary of last post

As you learned in the last blog post, using execute to send a request to a remote server interacts with the browser through WebDriver, and you can get some information by sending a defined command constant.

The execute method implementation was explained in the previous blog post. And we already know webdriver base class (. Selenium webdriver. Remote. Webdriver), implement the basic methods of operating elements on the page.

Comprehensive learning through simple application

Suppose you need to open Baidu and search for “CSDN A757291228”. How do you do it? By looking for webdriver base class (. Selenium webdriver. Remote. Webdriver) found the following search element method:

def find_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_)

    def find_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. An empty list if not :Usage: elements = driver.find_elements_by_id('foo') """
        return self.find_elements(by=By.ID, value=id_)

    def find_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)

    def find_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. An empty list if not :Usage: elements = driver.find_elements_by_xpath("//div[contains(@class, 'foo')]") """
        return self.find_elements(by=By.XPATH, value=xpath)

    def find_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)

    def find_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. an empty list if not :Usage: elements = driver.find_elements_by_link_text('Sign In') """
        return self.find_elements(by=By.LINK_TEXT, value=text)

    def find_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)

    def find_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. an empty 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)

    def find_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)

    def find_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. an empty list if not :Usage: elements = driver.find_elements_by_name('foo') """
        return self.find_elements(by=By.NAME, value=name)

    def find_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)

    def find_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. An empty list if not :Usage: elements = driver.find_elements_by_tag_name('h1') """
        return self.find_elements(by=By.TAG_NAME, value=name)

    def find_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)

    def find_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. An empty list if not :Usage: elements = driver.find_elements_by_class_name('foo') """
        return self.find_elements(by=By.CLASS_NAME, value=name)

    def find_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)

    def find_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. An empty list if not :Usage: elements = driver.find_elements_by_css_selector('.foo') """
        return self.find_elements(by=By.CSS_SELECTOR, value=css_selector)
def find_element(self, by=By.ID, value=None) :
        """ Find an element given a By strategy and locator. Prefer the find_element_by_* methods when possible. :Usage: element = driver.find_element(By.ID, 'foo') :rtype: WebElement """
        if self.w3c:
            if by == By.ID:
                by = By.CSS_SELECTOR
                value = '[id="%s"]' % value
            elif by == By.TAG_NAME:
                by = By.CSS_SELECTOR
            elif by == By.CLASS_NAME:
                by = By.CSS_SELECTOR
                value = ".%s" % value
            elif by == By.NAME:
                by = By.CSS_SELECTOR
                value = '[name="%s"]' % value
        return self.execute(Command.FIND_ELEMENT, {
            'using': by,
            'value': value})['value']

    def find_elements(self, by=By.ID, value=None) :
        """ Find elements given a By strategy and locator. Prefer the find_elements_by_* methods when possible. :Usage: elements = driver.find_elements(By.CLASS_NAME, 'foo') :rtype: list of WebElement """
        if self.w3c:
            if by == By.ID:
                by = By.CSS_SELECTOR
                value = '[id="%s"]' % value
            elif by == By.TAG_NAME:
                by = By.CSS_SELECTOR
            elif by == By.CLASS_NAME:
                by = By.CSS_SELECTOR
                value = ".%s" % value
            elif by == By.NAME:
                by = By.CSS_SELECTOR
                value = '[name="%s"]' % value

        # Return empty list if driver returns null
        # See https://github.com/SeleniumHQ/selenium/issues/4555
        return self.execute(Command.FIND_ELEMENTS, {
            'using': by,
            'value': value})['value'] or []
Copy the code

From the above implementation methods, execute method implementation is not described here, which has been explained in the previous section. This section mainly introduces the use of method.

First look at the use of the find_element_by_id method, which is described in the method specification:

element = driver.find_element_by_id('foo')
Copy the code

The method is annotated as follows (screenshots are used to illustrate the annotations below for clarity) :



According to the annotation, the find_element_by_id method finds the element whose ID is the specified value and returns that element.

Check the specific implementation as follows:

self.find_element(by=By.ID, value=id_)
Copy the code

The above implementation calls the find_element method and passes in by as by. ID, followed by the specific value; First check By class (selenium.webdriver.com mon. By) :

class By(object) :
    """ Set of supported locator strategies. """

    ID = "id"
    XPATH = "xpath"
    LINK_TEXT = "link text"
    PARTIAL_LINK_TEXT = "partial link text"
    NAME = "name"
    TAG_NAME = "tag name"
    CLASS_NAME = "class name"
    CSS_SELECTOR = "css selector"
Copy the code

This class with the Command mand (selenium.webdriver.remote.com) works in a similar way, the festival has show the Command here but also By many.

See the find_element method implementation here:

def find_element(self, =By.ID, value=None) :
    """ Find an element given a By strategy and locator. Prefer the find_element_by_* methods when possible. :Usage: element = driver.find_element(By.ID, 'foo') :rtype: WebElement """
    if self.w3c:
        if by == By.ID:
            by = By.CSS_SELECTOR
            value = '[id="%s"]' % value
        elif by == By.TAG_NAME:
            by = By.CSS_SELECTOR
        elif by == By.CLASS_NAME:
            by = By.CSS_SELECTOR
            value = ".%s" % value
        elif by == By.NAME:
            by = By.CSS_SEhj0ECTOR
            value = '[name="%s"]' % value
    return self.execute(Command.FIND_ELEMENT, {
        'using': by,
        'value': value})['value']
Copy the code

The above classes first determine the type of lookup, then concatenate the values, pass the lookup and value into the execute method, and then return the element object.

Almost all element lookup methods, implement the same, we simply use this function.

Before writing the code, we need to open baidu website, review the element to find the id value:



The input field id is kw, so the code should look like this:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
input = driver.find_element_by_id('kw')


# print (' author's blog: https://blog.csdn.net/A757291228)
# Support original, reprint please post a link
Copy the code

The element object is returned when the element is found:



In element class (selenium webdriver. Remote. Webelement) finds the method, find the following methods:

def send_keys(self, *value) :
    """Simulates typing into the element. :Args: - value - A string for typing, or setting form fields. For setting file 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 binding
    if self.parent._is_remote:
        local_file = self.parent.file_detector.is_local_file(*value)
        if local_file is not None:
            value = self._upload(local_file)

    self._execute(Command.SEND_KEYS_TO_ELEMENT,
                  {'text': "".join(keys_to_typing(value)),
                   'value': keys_to_typing(value)})
# Private Methods
    def _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. """
        if not params:
            params = {}
        params['id'] = self._id
        return self._parent.execute(command, params)
Copy the code

Send_keys is also sent through execute to get the result. The use of send_keys is shown in the comment:

form_textfield.send_keys("admin")
Copy the code

Let’s modify the previous code:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
input = driver.find_element_by_id('kw')
input.send_keys("CSDN A757291228")

# print (' author's blog: https://blog.csdn.net/A757291228)
# Support original, reprint please post a link
Copy the code

Finally check a click to complete the automatic search function; We continue to look at the element class and find the following method:

def click(self) :
        """Clicks the element."""
        self._execute(Command.CLICK_ELEMENT)
Copy the code

The click method is implemented the same as the send_keys method without further elaboration. Click on an element directly using the click method. Search baidu search click button ID:



Modify the code as follows:

from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")
input = driver.find_element_by_id('kw')
input.send_keys("CSDN A757291228")
enter = driver.find_element_by_id('su')
enter.click()
# print (' author's blog: https://blog.csdn.net/A757291228)
# Support original, reprint please post a link
Copy the code

The running results are as follows:

conclusion

We have simply learned how to use Selenium to open the browser and search for “CSDN A757191228”. In this simple example, we have learned not only the original lines of code of this example; Through the implementation of the analysis, to understand the location of other functions, you can use these functions to achieve their desired functions!

From the framework implementation analysis can get twice the result with half the effort the use of learning framework, as well as understanding the framework implementation principle, more conducive to our development and use.