我正在使用下面的代码来查找所有类值为=“ ng_isolate_scope”的元素。我需要做的是捕获所选元素的标签值,因为我需要此信息以进行进一步分析
<span class="ng-isolate-scope">
<div class="ng-isolate-scope">
代码:
elems = driver.find_elements_by_class_name("ng-isolate-scope")
for elem in elems:
tag_value = elem.get_tag()
print("element found with tag value = " + str(tag_value))
但是,tag_value()不存在。如何捕获元素的标签值? 谢谢
答案 0 :(得分:1)
已更新: 有点棘手,这里我的方法是获取element的outerHTML,然后拆分第一个单词(即标记名)。因此,您可以尝试:
elements = driver.find_elements_by_class_name("ng-isolate-scope")
for element in elements:
outerhtml = element.get_attribute('outerHTML ') // to extract outerHTML
tag_value=outerhtml.split('',1)[0] // to extract first word
print("element found with tag value = " + tag_value)
答案 1 :(得分:0)
如果我没看错,您想要一个标签文本:
elems = driver.find_elements_by_class_name("ng-isolate-scope")
for elem in elems:
tag_value = elem.text # get text of an element
print("'element found with tag value = " + tag_value + "'")
例如:
<span class="ng-isolate-scope">span tag</span>
<div class="ng-isolate-scope">div tag</div>
将产生:
'element found with tag value = span tag'
'element found with tag value = div tag'
编辑:
from bs4 import BeautifulSoup
from html.parser import HTMLParser
driver = webdriver.Chrome()
driver.get("https://stackoverflow.com/questions/51789088/python-selenium-get-tag-value-of-a-selected-element/51789139#51789139")
parsed_html = BeautifulSoup(driver.page_source) # get HTML
list = list() # create a list
class MyHTMLParser(HTMLParser):
def handle_starttag(self, tag, attrs): # get start tags
list.append(tag) # store them in the list
parser = MyHTMLParser()
parser.feed(str(parsed_html.body.find('div', attrs={'id':'question-header'}))) # feed parser with parsed HTML
print(list[0]) # first element in the list is the tag you need
输出:
div