selenium使用手机浏览模式打开chrome,进行浏览网页(java)

测一些手机网页的自动化非常好用。

  • 下面是java执行代码。使用谷歌模拟手机,进行浏览网页。运行时注意chromedriver.exe的安装路径。
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

import java.util.HashMap;

public class temp {

    public static void main(String[] args) {
        System.setProperty("webdriver.chrome.driver", ".\\drivers\\chromedriver.exe");
        String URL = "https://www.baidu.com";

        HashMap<String,String>  mobileEmulation = new HashMap<String,String>();
        mobileEmulation.put("deviceName","iPhone X");
        ChromeOptions options = new ChromeOptions();
        options.setExperimentalOption("mobileEmulation", mobileEmulation);
        WebDriver driver = new ChromeDriver(options);
        driver.get(URL);    //进入目的链接
    }
}
  • python执行代码
from selenium import webdriver

mobileEmulation = {'deviceName': 'Apple iPhone 4'}
options = webdriver.ChromeOptions()
options.add_experimental_option('mobileEmulation', mobileEmulation)
driver = webdriver.Chrome(executable_path='chromedriver.exe', chrome_options=options)
driver.get('http://m.baidu.com')
发布了17 篇原创文章 · 获赞 9 · 访问量 1849

猜你喜欢

转载自blog.csdn.net/qq_45731111/article/details/104707337