chromedriver_selenium_java2(图片,JavaScript)

图片禁止加载, JavaScript禁用


1.查看官网:https://sites.google.com/a/chromium.org/chromedriver/capabilities

打开Preferences文件,



设置chrome浏览器, 关闭JavaScript, 关闭图片加载可以看见

Preferences文件变化:

            },
                "subresource_filter": {},
                "usb_chooser_data": {}
            },
            "pref_version": 1
        },
        "default_content_setting_values": {
            "images": 2,
            "javascript": 2
        },
        "exit_type": "Crashed",
        "exited_cleanly": true,
        "gaia_info_picture_url": "https://lh3.googleusercontent.com/-XdUIqdMkCWA/AAAAAAAAAAI/AAAAAAAAAAA/4252rscbv5M/s256-c/photo.jpg",
        "gaia_info_update_time": "13146664779616686",
        "icon_version": 4,
        "last_engagement_time": "13146675621832549",
        "managed_user_id": "",
        "managed_users": {},
        "name": "用户1",
        "password_manager_enabled": true,



2. java代码1:

			Map<String, Object> contentSettings = new HashMap<String, Object>();  
			contentSettings.put("images", 2);
			contentSettings.put("javascript", 2);
			  
			Map<String, Object> preferences = new HashMap<String, Object>();  
			preferences.put("profile.default_content_setting_values", contentSettings);  
			  
			
			
			DesiredCapabilities caps = DesiredCapabilities.chrome(); 
			caps.setCapability("prefs", preferences);
			
			driver = new ChromeDriver(caps);
			driver.get(url);
			System.out.println(driver.findElement(By.className("border_in")).getText());
发现只有图片禁止了加载, JavaScript还是照样运行, 百思不得骑姐. 看下面


3. java代码2:

			Map<String, Object> contentSettings = new HashMap<String, Object>();  
			contentSettings.put("images", 2);
			contentSettings.put("javascript", 2);
			  
			Map<String, Object> preferences = new HashMap<String, Object>();  
			preferences.put("profile.default_content_setting_values", contentSettings);  
			  
			ChromeOptions options= new ChromeOptions();
			options.addArguments("--test-type --no-sandbox");
			options.addArguments("--enable-strict-powerful-feature-restrictions");
			
			options.setExperimentalOption("prefs", preferences);
			
			DesiredCapabilities caps = DesiredCapabilities.chrome();  
			caps.setCapability(ChromeOptions.CAPABILITY, options);  

			
			driver = new ChromeDriver(caps);
			driver.get(url);
			System.out.println(driver.findElement(By.className("border_in")).getText());


最终, JavaScript 和图片都禁止加载了, 作者使用的chrome版本是59.0, 在设置方面可能不同版本会差异比较大.






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值