Home > Back-end >  Short form of find_element Python Selenium
Short form of find_element Python Selenium

Time:05-04

I just upgraded to Selenium 4. This resulted in Depreciation warnings from my Python scripts about calls to find_element_by_xxxxx().

DeprecationWarning: find_element_by_name is deprecated. Please use find_element(by=By.NAME, value=name) instead Select(driver1.find_element_by_name("age_max")).select_by_index(81)

Select(driver1.find_element(by=By.NAME, value="age_max")).select_by_index(81)

But it seems that this works.

Select(driver1.find_element(By.NAME, "age_max")).select_by_index(81)

Why does this work?

Why do they suggest the use of "by=" and "value=" when they are not required?

Is this going to cause issues in future?

Are they trying to make Python function args into some sort of attribute?

CodePudding user response:

It's a deprecation warning, the deprecation hasn't been fully rolled out yet. They're letting you know that you should swap to this new form across your code-base soon, to avoid problems when the changes are made.

CodePudding user response:

If you look at the internal code of find_element

def find_element(self, by=By.ID, value=None):
    """
    Find an element given a By strategy and locator. 

    :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']

In the newer version of selenium (Selenium V4.0)

find_element_by_xxxxx()

have been deprecated, so one would have to use find_element(By.**, "value") to find an web element.

Why does this work?

  • cause first arg has to by By type and second has to be value

also, if you pay attention, they are converting ID, TAG_NAME, CLASS_NAME, NAME, to by = By.CSS_SELECTOR

Is this going to cause issues in the future? - No

  • Related