首页 > 解决方案 > 如何使用 Beautiful 或 Selenium 在 Python 中基于 div id 获取 aria-label 作为文本

问题描述

<div id="i19" class="quantumWizTogglePapercheckboxEl appsMaterialWizTogglePapercheckboxCheckbox docssharedWizToggleLabeledControl freebirdThemedCheckbox freebirdThemedCheckboxDarkerDisabled freebirdMaterialWidgetsToggleLabeledCheckbox isCheckedNext isChecked" jscontroller="EcW08c" jsaction="keydown:I481le;dyRcpb:dyRcpb;click:cOuCgd; mousedown:UX7yZ; mouseup:lbsD7e; mouseenter:tfO1Yc; mouseleave:JywGue; focus:AHmuwe; blur:O22p3e; contextmenu:mg9Pef;touchstart:p6p2H; touchmove:FwuNnf; touchend:yfqBxc(preventMouseEvents=true|preventDefault=true); touchcancel:JMtRjd;" jsshadow="" jsname="FkQz1b" aria-label="Al-Baraka Bank Limited" data-answer-value="Al-Baraka Bank (Pakistan) Limited" role="checkbox" aria-checked="true" tabindex="0">

嗨,我正在尝试Al-Baraka Bank Limited使用 div id获取放置在此 div 中的 aria-label 内的文本i19。基本上我点击了一个复选框,我希望将文本(即 aria-label)保存到一个变量中,以便我可以在其他地方再次使用它。

标签: pythonhtmlseleniumweb-scrapingbeautifulsoup

解决方案


如果该idunique,那么您可以get_attribute获取属性值。

aria_label = driver.find_element(By.ID, "i19").get_attribute('aria-label')
print(aria_label)

推荐阅读