gpt4 book ai didi

python - 当我调用以下名为 find_element_by_xpath 的函数时到底发生了什么?

转载 作者:行者123 更新时间:2023-12-01 04:29:06 24 4
gpt4 key购买 nike

我对以下方法有疑问..

 driver.find_element_by_xpath

当我通过 ide 导航时,我遇到了以下调用。 ->

def find_element_by_xpath(self, xpath):
"""
Finds an element by xpath.

:Args:
- xpath - The xpath locator of the element to find.

:Usage:
driver.find_element_by_xpath('//div/td[1]')
"""
return self.find_element(by=By.XPATH, value=xpath)

然后当我导航到“find_element”时,我得到以下内容

def find_element(self, by=By.ID, value=None):
"""
'Private' method used by the find_element_by_* methods.

:Usage:
Use the corresponding find_element_by_* instead of this.

:rtype: WebElement
"""
if not By.is_valid(by) or not isinstance(value, str):
raise InvalidSelectorException("Invalid locator values passed in")
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']

最后我得到了以下内容

def execute(self, driver_command, params=None):
"""
Sends a command to be executed by a command.CommandExecutor.

:Args:
- driver_command: The name of the command to execute as a string.
- params: A dictionary of named parameters to send with the command.

:Returns:
The command's JSON response loaded into a dictionary object.
"""
if self.session_id is not None:
if not params:
params = {'sessionId': self.session_id}
elif 'sessionId' not in params:
params['sessionId'] = self.session_id

params = self._wrap_value(params)
response = self.command_executor.execute(driver_command, params)
if response:
self.error_handler.check_response(response)
response['value'] = self._unwrap_value(
response.get('value', None))
return response
# If the server doesn't send a response, assume the command was
# a success
return {'success': 0, 'value': None, 'sessionId': self.session_id}

当我如下使用此功能时

apply = driver.find_element_by_xpath("//*@id='maincontent']/form/div[3]/input")

这里我存储了要应用的该函数的返回值以及当我查找类型(应用)时的返回值。我得到以下类型

<class 'selenium.webdriver.remote.webelement.WebElement'>

即使我也能够对类方法执行多个相同的操作,如下所示

driver.find_element_by_xpath("//*[@id='cbid.vpn-g.VPN_Type.type']").find_element_by_id("cbi-vpn-g-VPN_Type-type-pptp").click()

谁能给我解释一下吗?

最佳答案

find_element_by_xpath() 和其他 find_element_by_* 方法基本上是主 find_element() 方法的便捷快捷方式/包装器,该方法将发送 findElement WebDriver command通过 JSON Wire protocol(基于 HTTP 的 RESTful JSON)。

如果找到一个元素,find_element() 返回一个 WebElement 实例,该实例本身具有上述所有方法并表示一个 DOM 元素。如果指定的定位器未找到某个元素,则会抛出 NoSuchElementException

关于python - 当我调用以下名为 find_element_by_xpath 的函数时到底发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32642780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com