如何使用selenium 驱动chrome浏览器并且打开方式为手机模式

随着移动设备使用率的不断增加,移动页面的测试也变得越来越重要。

对于互联网公司M站的测试,如果不通过专用的appium等移动端测试工具是否还有方便快捷的办法呢?答案当然是有啊。

使用chrome driver和chrome浏览器并进入chrome的 toggle device mode 模式,就可以很好的模拟手机端,下面直接上代码。

import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

public class TestH5 {
    public static void main(String args[]) {
        System.setProperty("webdriver.chrome.driver", "E:/Selenium/2016-07-14/chromedriver.exe");
        Map<String, String> mobileEmulation = new HashMap<String, String>();
        mobileEmulation.put("deviceName", "Google Nexus 5");
        Map<String, Object> chromeOptions = new HashMap<String, Object>();
        chromeOptions.put("mobileEmulation", mobileEmulation);
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, chromeOptions);
        WebDriver driver = new ChromeDriver(capabilities);
        Navigation navigation = driver.navigate();
        navigation.to("https://m.baidu.com/");
        String title = driver.getTitle();
        System.out.println("title:" + title);
    }
}

使用的chrome版本和chromedriver版本,这两个如果配合不好会有坑。

 

Chromedriver版本为2.53.1

转载:http://www.cnblogs.com/iamhp/p/6016194.html

转载于:https://www.cnblogs.com/sunnytest/p/7353995.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Selenium复用Chrome浏览器,你可以按照以下步骤进行操作: 1. 将SeleniumChrome浏览器驱动放到Python的script目录下。 2. 在命令行下执行chromedriver,以开启Chrome浏览器驱动。 3. 开启浏览器远程调试端口,可以自定义端口号,确保不与本地已开端口冲突。例如执行以下命令:`/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome -remote-debugging-port=9222`。 4. 在Selenium脚本中,使用`Options()`对象来设置复用浏览器的IP地址和端口号。例如,在`options.debugger_address`中设置为"127.0.0.1:9222"。 5. 创建Chrome浏览器的WebDriver对象,并传入设置好的options。 6. 执行测试,通过`driver`对象对当前已经打开的页面进行操作。例如,使用`driver.find_element(By.ID, "kw").send_keys("selenium")`来在百度搜索框中输入关键词"selenium"。 这样就可以实现使用Selenium复用Chrome浏览器进行页面操作了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [seleniumChrome浏览器驱动](https://download.csdn.net/download/saqin6255/87781171)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [selenium复用浏览器进行调试操作如何去做](https://blog.csdn.net/abcnull/article/details/106696866)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [【selenium】复用浏览器(debugger)](https://blog.csdn.net/lan_yangbi/article/details/127970162)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值