拥有以下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这样的东西。
答案 0 :(得分: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上。
P.S。这可以进一步增强,例如将空白与&lt;,&gt;一起剥离。或/.
答案 1 :(得分:0)
这并不容易。
每个元素都有一系列隐式属性以及明确定义的属性(例如选中,禁用等)。因此,我能想到的唯一方法是获取对父项的引用,然后使用JavaScript执行程序来获取innerHTML:
document.getElementById('{ID of element}').innerHTML
然后,您必须解析innerHTML返回的内容以提取单个元素,然后一旦您隔离了您感兴趣的元素,您将再次解析该元素以提取属性列表。