私信  •  关注

PDHide

PDHide 最近创建的主题
PDHide 最近回复了
4 年前
回复了 PDHide 创建的主题 » 如何在移动仿真模式下运行Selenium Python?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button id="test">TEST</button>

<script type="text/javascript">
        $('#test').on('touchend click',function(e){
  if(e.type=='click')
    alert('click triggered');
  else
    alert('touch triggered');
});
    </script>

将上面的html复制到一个文件中,然后从selenium打开它。

现在运行代码,你可以看到你得到触摸触发警报,而不是点击触发:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
mobile_emulation = {
    "deviceMetrics": {"width": 360, "height": 640, "pixelRatio": 3.0},
    "userAgent": "Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19"}


chrome_options = webdriver.ChromeOptions()

chrome_options.add_experimental_option(
    "mobileEmulation", mobile_emulation)

driver = webdriver.Chrome(options=chrome_options)

driver.get(
    "file:///C:/Users/prave/Desktop/push.html")

driver.find_element_by_tag_name("button").click()
input()

输出:

该浏览器看起来像普通浏览器,但当您打开任何url usign驱动程序时,屏幕分辨率将是移动设备的分辨率。得到(“”)

enter image description here