python - 用 selenium 和 Python 读出 div 中的类名
问题描述
正如你在图片中看到的,我想检查一个棋盘。为此,我使用 Python 和 Selenium。我的愿望是有一个功能,我可以一个接一个地传递国际象棋领域的名称,例如g1,g2,g3......网站上有ID与这些名称匹配的对象。现在我想找回场上人物的名字。该名称在对象的类名中。但是,在一个 div 中。
(我确实取回了类名“lcs black ui-droppable”,但我确实想取回了类名“lichess piece knight white ui-draggable”)
我的问题是如何提取这个类名。
这是我的代码:
def getFigure(position):
figure = driver.find_element_by_id(position)
name = figure.get_attribute("class")
print(name)
我确实将字段的名称传递到位置变量中。
谢谢你的提示!
解决方案
print(figure.find_element_by_xpath("./div[2]").get_attribute("class"))
您可以从父元素 xpath 并获取第二个 div。
推荐阅读
- c# - ASP.NET Core 2.2 是否支持使用 Microsoft 帐户进行外部登录?
- java - 为什么@autowired 不适用于同一包中的服务以及如何修复它?
- java - 需要一个无法找到的 Repository 类型的 bean
- javascript - Javascript:无法获取用户输入并在另一个函数中运行它
- php - PHP SQL使用复杂的复选框过滤器获取值
- java - 使用 iText 7 设置空间宽度
- outlook - Outlook 加载项因“MeetingSuggestion”清单实体类型和电子邮件正文中的某些字符串而崩溃
- python - 使用python中的分隔符列表将列表拆分为子列表
- javascript - 无法将我的答案与我的测验问题联系起来
- javascript - 如何在 d3.json() 中调用函数