如何在 Playwright Python 中打印元素文本?

问题描述 投票:0回答:1

如何在playwright python中打印元素文本?

self.error_alert_text = page.locator("#alert .modal > p")

我已经尝试过

print(self.error_alert_text.get_by_text()))

但是什么也没有打印出来。如何打印元素值?

另外,如何将字符串与打印对象连接起来?即:

print("test print" + self.error_alert_text.get_by_text()))
python automation automated-tests playwright
1个回答
1
投票

get_by_text()
方法不获取文本,它获取具有方法参数中定义的文本的
ElementHandle

要获取警报文本,您应该使用

text_content()
inner_text()
上的
ElementHandle

page.locator('#alert .modal > p').wait_for(state='visible')
print(f"Alert text: {page.locator('#alert .modal > p').text_content()}")

page.locator('#alert .modal > p').wait_for(state='visible')
print(f"Alert text: {page.locator('#alert .modal > p').inner_text()}")

文字内容参考

内部文本参考

© www.soinside.com 2019 - 2024. All rights reserved.