"find_element_by_name('name')"和&QOOT;FIND_ELEMENT(BY NAME,'NAME')"之间有什么区别?

What is the difference between quot;find_element_by_name(#39;name#39;)quot; and quot;find_element(By.NAME, #39;name#39;)quot;?(quot;find_element_by_name(#39;name#39;)quot;和QOOT;FIND_ELEMENT(BY NAME,#39;NAME#39;)QUOT;之间有什么区别?)

本文介绍了"find_element_by_name('name')"和&QOOT;FIND_ELEMENT(BY NAME,'NAME')"之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:

# 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@AutomatedTestermentions:

决定简化跨语言的API,这就做到了这一点。

这篇关于"find_element_by_name('name')"和&QOOT;FIND_ELEMENT(BY NAME,'NAME')"之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:"find_element_by_name('name')"和&QOOT;FIND_ELEMENT(BY NAME,'NAME')"之间有什么区别?

基础教程推荐