Python selenium获取所选元素的标记值

qvtsj1bj  于 2023-01-20  发布在  Python
关注(0)|答案(3)|浏览(153)

我使用下面的代码来查找class value ="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()不存在,我该怎么做才能捕捉到元素的标签值呢?谢谢

t5fffqht

t5fffqht1#

updated:有点棘手,这里我的方法是获取元素的outterHTML,然后拆分第一个单词(标记名)。所以你可以尝试:

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)
xghobddn

xghobddn2#

如果我没理解错的话,你需要一个标签的文本:

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

参考documentation

wgxvkvu9

wgxvkvu93#

您可以通过直接获取tagName属性来完成此操作,而无需解析outerHTML:

elements = driver.find_elements_by_class_name("ng-isolate-scope")
for element in elements:
  tag_name = element.get_attribute('tagName')
  print(tag_name) ## INPUT / A / BUTTON

相关问题