(Ix) the positioning element WebDriver API - By using positioning

Essays and records to facilitate their access to fellow travelers.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

  Learning before selenium automation, it is best to learn HTML, CSS, JavaScript and other knowledge, help to understand the principles of operation and positioning elements. About python and selenium install their own search for other information,

Here do not introduced, all examples use python3.6 + selenium execution.

#------------------------------------------------I ------------------------------------------- dividing line is a shame

 

By By positioning

Described earlier for the 8 positioning method, the webdriver also provides another set of writing, i.e. with a uniform find_element () method, By declare positioning method, and positioning corresponding to the incoming reference positioning method. details as follows:

find_element(By.ID,”kw”)

find_element(By.NAME,”WD”)

find_element(By.CLASS_NAME,”s_ipt”)

find_element(By.TAG_NAME,”input”)

find_element (By.LINK_TEXT, "News")

find_element(By.PARTIAL_LINK_TEXT,”新”)

find_element(By.XPATH,”//*[@class=’bg s_btn’]”)

find_element(By.CSS_SELECTOR,”span.bg s_btn_wr>input#su”)

find_element () method is only used to locate the element, which requires two parameters, the first parameter type is positioned by By providing; DETAILED second parameter is located. In use By necessary before By classes into.

from selenium.webdriver.common.by import By

By looking at WebDriver underlying implementation code and realize they are all about children, for example, find_element_by_id () implementation 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_)

 

But WebDriver more recommended wording previously described

 

Guess you like

Origin www.cnblogs.com/lirongyang/p/11457510.html