元素定位
- query_selector(engine=body) # 选择单个元素
- query_selectorAll(engine=body) # 选择多个元素
- wait_for_selector(engine=body) # 选择单个元素,并且自动等待到元素可见、可操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| 1. 以
pp = page.query_selector("xpath=//h2")
pp = page.query_selector("//h2")
2. 以引号 "" 或者 ' 开头的,判断为text
ppp = page.query_selector("text=文本输入") \
ppp = page.query_selector("'文本输入'")
注意:双引号里有一个单引号,不然无法识别
3. 其他的,都判断为css
p = page.query_selector("css=h2")
p = page.query_selector("h2")
|
打印元素 html,方便定位测试
1 2 3 4 5
| element_handle.inner_html() returns: <str># Returns the element.innerHTML. element_handle.inner_text()
|
参考:http://blog.itpub.net/69942496/viewspace-2765795/
如何使用 mobile 仿真模式,支持 touch 事件
1 2
| const devtoolsProtocolClient = await page.target().createCDPSession(); await devtoolsProtocolClient.send("Emulation.setEmitTouchEventsForMouse", { enabled: true });
|
playwright
1 2
| client = page.context.new_cdp_session(page) client.send("Emulation.setEmitTouchEventsForMouse",{"enabled":True})
|
参考:https://github.com/puppeteer/puppeteer/issues/1079
执行 js
1 2
| page.evaluate("success()") page.evaluate('document.querySelectorAll(".add")[' + str(i) + '].click()')
|