'Short form of find_element Python Selenium [duplicate]
I just upgraded to Selenium 4. This resulted in Depreciation warnings from my Python scripts about calls to find_element_by_xxxxx(). the warning suggests this form.
Select(driver1.find_element(by=By.NAME, value="age_max")).select_by_index(81)
But it seems that this works as well.
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?
Solution 1:[1]
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.
Solution 2:[2]
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
arghas to byBy typeand second has to bevalue
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Inigo Selwood |
| Solution 2 | cruisepandey |
