playwright的元素定位方式有很多种,比selinum的8大定位方式更多,定位器locator通过传入的选择器selector参数进行定位,所以定位方式很多的根本是选择器的选择方式还有很多种,今天讲解文本选择器。
文本选择器:Text Selector
文本选择器定位包含某些文本的元素,有下面两种方式:
- page.locator("text=Log in").click()
- page.locator("text='Log in'").click()
1)text=Log in 和 text='Log in' 的区别:
Log in 没有加引号(单引号或者双引号):模糊匹配 并 对大小写不敏感的
'Log in' 有引号:精确匹配 并 对大小写敏感
text=Log
匹配 <button>Log in</button>
或者 <button>LOG in</button>
text='Log
' 不匹配 <button>Log in</button>
也不匹配 <button>log</button>
2)匹配时会将多个空格格式化为一个空格,并去掉头尾空格,比如:
text='Log in' 可以匹配 <button> Log in </button>
3)当文本中本身就有引号时,需要将引号进行转义&#