示例:
# 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')
基本上,我想知道:
1-两者之间有什么不同?如果有,是什么?
2-一般来说,这两者有什么区别吗?
find_element_by_class_name(el): find_element(By.CLASS_NAME, el);
find_element_by_name(el): find_element(By.NAME, el)
3-执行第一个方法时为什么显示DeprecationWarning
?
正如@guihva也提到的,这两行没有区别:
driver.find_element_by_name('q')
和
driver.find_element(By.NAME, 'q')
如同当前版本的selenium4Python客户端find_element_by_name(name)
在幕后仍然调用:
self.find_element(by=By.NAME, value=name)
但与DeprecationWarning一起。
find_element_by_name()当前实现如下:
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)
As@AutomatedTester
mentions:
决定简化跨语言的API,这就做到了这一点。
这篇关于";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!