我们知道selenium可以通过模拟UA实现模拟手机。而playwright更是自带对于移动设备的模拟。那么我们怎么知道都可以模拟哪些设备呢?
代码示例
from playwright.sync_api import Playwright, sync_playwright
def run(playwright: Playwright) -> None:
iPad = playwright.devices['Galaxy S5']
browser = playwright.chromium.launch(headless=False)
context= browser.new_context(**iPad)
page = context.new_page()
page.goto("https://m.baidu.com")
page.wait_for_timeout(10000000)
page.close()
context.close()
browser.close()
with sync_playwright() as playwright:
run(playwright)
以上就是模拟Galaxy S5,UA和分辨率都设置好了,就不需要再设置了
然后,如果我们需要获取所有可以模拟的设备,可以用devices.keys()方法
from playwright.sync_api import Playwright, sync_playwright
def run(playwright: Playwright) -> None:
devices_keys = playwright.devices.keys()
print(list(devices_keys))
with sync_playwright() as playwright:
run(playwright)
获取设备的列表,结果为:
['Blackberry PlayBook', 'Blackberry PlayBook landscape', 'BlackBerry Z30', 'BlackBerry Z30 landscape', 'Galaxy Note 3', 'Galaxy Note 3 landscape', 'Galaxy Note II', 'Galaxy Note II landscape', 'Galaxy S III', 'Galaxy S III landscape', 'Galaxy S5', 'Galaxy S5 landscape', 'Galaxy S8', 'Galaxy S8 landscape', 'Galaxy S9+', 'Galaxy S9+ landscape', 'Galaxy Tab S4', 'Galaxy Tab S4 landscape', 'iPad (gen 6)', 'iPad (gen 6) landscape', 'iPad (gen 7)', 'iPad (gen 7) landscape', 'iPad Mini', 'iPad Mini landscape', 'iPad Pro 11', 'iPad Pro 11 landscape', 'iPhone 6', 'iPhone 6 landscape', 'iPhone 6 Plus', 'iPhone 6 Plus landscape', 'iPhone 7', 'iPhone 7 landscape', 'iPhone 7 Plus', 'iPhone 7 Plus landscape', 'iPhone 8', 'iPhone 8 landscape', 'iPhone 8 Plus', 'iPhone 8 Plus landscape', 'iPhone SE', 'iPhone SE landscape', 'iPhone X', 'iPhone X landscape', 'iPhone XR', 'iPhone XR landscape', 'iPhone 11', 'iPhone 11 landscape', 'iPhone 11 Pro', 'iPhone 11 Pro landscape', 'iPhone 11 Pro Max', 'iPhone 11 Pro Max landscape', 'iPhone 12', 'iPhone 12 landscape', 'iPhone 12 Pro', 'iPhone 12 Pro landscape', 'iPhone 12 Pro Max', 'iPhone 12 Pro Max landscape', 'iPhone 12 Mini', 'iPhone 12 Mini landscape', 'iPhone 13', 'iPhone 13 landscape', 'iPhone 13 Pro', 'iPhone 13 Pro landscape', 'iPhone 13 Pro Max', 'iPhone 13 Pro Max landscape', 'iPhone 13 Mini', 'iPhone 13 Mini landscape', 'JioPhone 2', 'JioPhone 2 landscape', 'Kindle Fire HDX', 'Kindle Fire HDX landscape', 'LG Optimus L70', 'LG Optimus L70 landscape', 'Microsoft Lumia 550', 'Microsoft Lumia 550 landscape', 'Microsoft Lumia 950', 'Microsoft Lumia 950 landscape', 'Nexus 10', 'Nexus 10 landscape', 'Nexus 4', 'Nexus 4 landscape', 'Nexus 5', 'Nexus 5 landscape', 'Nexus 5X', 'Nexus 5X landscape', 'Nexus 6', 'Nexus 6 landscape', 'Nexus 6P', 'Nexus 6P landscape', 'Nexus 7', 'Nexus 7 landscape', 'Nokia Lumia 520', 'Nokia Lumia 520 landscape', 'Nokia N9', 'Nokia N9 landscape', 'Pixel 2', 'Pixel 2 landscape', 'Pixel 2 XL', 'Pixel 2 XL landscape', 'Pixel 3', 'Pixel 3 landscape', 'Pixel 4', 'Pixel 4 landscape', 'Pixel 4a (5G)', 'Pixel 4a (5G) landscape', 'Pixel 5', 'Pixel 5 landscape', 'Moto G4', 'Moto G4 landscape', 'Desktop Chrome HiDPI', 'Desktop Edge HiDPI', 'Desktop Firefox HiDPI', 'Desktop Safari', 'Desktop Chrome', 'Desktop Edge', 'Desktop Firefox']
其中,列表中最后带Desktop的是桌面设备,删掉即可,剩余大概108个设备
转载请注明:思享SEO博客 » playwright可以仿真哪些移动设备?