我对以下方法有疑问..
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")
这里我存储此函数的返回值以及当我查找类型(apply)时。我得到以下类型
<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()
任何人都可以向我解释一下吗?
答案 0 :(得分:2)
find_element_by_xpath()
和其他find_element_by_*
方法基本上是方便的主要find_element()
方法的快捷方式/包装器,它会通过findElement
WebDriver command发送JSON Wire protocol(RESTful JSON)通过HTTP)。
如果找到一个元素,find_element()
将返回一个WebElement
实例,该实例本身具有上述所有方法并代表一个DOM元素。如果指定的定位器找不到元素,则会抛出NoSuchElementException
。