3

具有以下 HTML 代码:

<span class="warning" id ="warning">WARNING:</span>

对于 XPATH 可访问的对象:

.//*[@id='unlink']/table/tbody/tr[1]/td/span

如何通过 Selenium WebDriver + Python 2.7 计算其属性(class、id),而不知道它们的名称?

我期待像count = 2这样的东西。

4

2 回答 2

2

知道了!这应该适用于 div、span、img、p 和许多其他基本元素。

element = driver.find_element_by_xpath(xpath) #Locate the element.

outerHTML = element.get_attribute("outerHTML") #Get its HTML
innerHTML = element.get_attribute("innerHTML") #See where its inner content starts

if len(innerHTML) > 0: # Let's make this work for input as well
    innerHTML = innerHTML.strip() # Strip whitespace around inner content
    toTrim = outerHTML.index(innerHTML) # Get the index of the first part, before the inner content
    # In case of moste elements, this is what we care about
    rightString = outerHTML[:toTrim]
else:
    # We seem to have something like <input class="bla" name="blabla"> which is good
    rightString = outerHTML
# Ie: <span class="something" id="somethingelse">

strippedString = rightString.strip() # Remove whitespace, if any
rightTrimmedString = strippedString.rstrip('<>') #
leftTrimmedString = rightTrimmedString.lstrip('</>') # Remove the <, >, /, chars.
rawAttributeArray = leftTrimmedString.split(' ') # Create an array of:
# [span, id = "something", class="somethingelse"]

curatedAttributeArray = [] # This is where we put the good values
iterations = len(rawAttributeArray)

for x in range(iterations):
    if "=" in rawAttributeArray[x]: #We want the attribute="..." pairs
        curatedAttributeArray.append(rawAttributeArray[x]) # and add them to a list

numberOfAttributes = len(curatedAttributeArray) #Let's see what we got
print numberOfAttributes # There we go

我希望这有帮助。

谢谢,R。

PS 这可以进一步增强,例如将空格与 <、> 或 / 一起剥离。

于 2013-03-21T09:23:13.920 回答
0

这并不容易。

每个元素都有一系列隐含的属性以及明确定义的属性(例如选择、禁用等)。因此,我能想到的唯一方法是获取对父级的引用,然后使用 JavaScript 执行器来获取 innerHTML:

document.getElementById('{ID of element}').innerHTML

然后,您必须解析 innerHTML 返回的内容以提取单个元素,然后一旦隔离了您感兴趣的元素,您将再次解析该元素以提取属性列表。

于 2013-03-21T06:45:01.783 回答