Issue
Examples:
# method 1
from selenium import webdriver
PATH = '...'
driver = webdriver.Chrome(PATH)
driver.get('https://google.com')
driver.find_element_by_name('q').send_keys('test')
# method 2
from selenium import webdriver
from selenium.webdriver.common.by import By
PATH = 'c:\\Program Files (x86)\\chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://google.com')
driver.find_element(By.NAME, 'q').send_keys('test')
Basically, I want to know:
1 - Are there differences between the two? If there are, what are they?
2 - Generally speaking, are there differences between these?
find_element_by_class_name(el): find_element(By.CLASS_NAME, el);
find_element_by_name(el): find_element(By.NAME, el)
3 - Why is a DeprecationWarning
shown when the first method is executed?
Solution
As @GuiHva also mentioned, there is no difference between the two lines:
driver.find_element_by_name('q')
and
driver.find_element(By.NAME, 'q')
as in the current release of selenium4 Python client find_element_by_name(name)
under the hood still invokes:
self.find_element(by=By.NAME, value=name)
but along with a DeprecationWarning.
The current implementation of find_element_by_name() is as follows:
def find_element_by_name(self, name) -> WebElement:
"""
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')
"""
warnings.warn(
"find_element_by_* commands are deprecated. Please use find_element() instead",
DeprecationWarning,
stacklevel=2,
)
return self.find_element(by=By.NAME, value=name)
Why this change
As @AutomatedTester
mentions:
The decision was to simplify the APIs across the languages and this does that.
Answered By - undetected Selenium
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.