首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在python中使用selenium找到这个跨度?

如何在python中使用selenium找到这个跨度?
EN

Stack Overflow用户
提问于 2020-01-19 11:06:17
回答 1查看 163关注 0票数 0

在python中使用selenium时,我想知道如何从这个div中获取span文本。

代码语言:javascript
运行
复制
<div class="clsFrameworkElement" 
id="ManagedQuestionViewer.brdrQuestion1.QuestionRenderer.MultiChoiceQuestionViewer.sp.StackPanel.RadioTextRegionViewer2.LayoutRoot.Panel.text.sp.rich.LayoutRoot.TextBlock" style="overflow: visible; display: inherit; text-shadow: white 0px 1px; left: 46px; top: 105px; border-width: 0px; width: 186px; height: 15px; text-align: left; color: rgb(0, 0, 0); font-family: Tahoma; font-weight: normal; font-size: 9.6pt; text-overflow: clip; overflow-wrap: break-word; white-space: normal; user-select: none;">
<span style="">Get a non-disclosure agreement.</span>
</div>

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-01-19 16:51:30

您可以使用xpath来获取span元素。

请看下面的代码片段:

代码语言:javascript
运行
复制
from selenium import webdriver

driver =  webdriver.Chrome()
html_content = """
<html>
     <head></head>
     <body>
         <div class="clsFrameworkElement" id="ManagedQuestionViewer.brdrQuestion1.QuestionRenderer.MultiChoiceQuestionViewer.sp.StackPanel.RadioTextRegionViewer2.LayoutRoot.Panel.text.sp.rich.LayoutRoot.TextBlock" style="overflow: visible; display: inherit; text-shadow: white 0px 1px; left: 46px; top: 105px; border-width: 0px; width: 186px; height: 15px; text-align: left; color: rgb(0, 0, 0); font-family: Tahoma; font-weight: normal; font-size: 9.6pt; text-overflow: clip; overflow-wrap: break-word; white-space: normal; user-select: none;">
            <span style="">Get a non-disclosure agreement.</span>
        </div>
     </body>
</html>
"""

driver.get("data:text/html;charset=utf-8,{html_content}".format(html_content=html_content))

node = driver.find_element_by_xpath("//div[@class='clsFrameworkElement']/span")
print(node.text)

这将打印以下内容:

代码语言:javascript
运行
复制
Get a non-disclosure agreement.

您可以在此处阅读有关xpath的更多信息:https://selenium-python.readthedocs.io/locating-elements.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59806809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档