selenium设置火狐相关配置
设置火狐的最大放大值 ,可以打开about:config后,使用zoom.maxPercent配置放大值 ,如:zoom.maxPercent设置为130
如果使用selenium,用这两种设置方法可以实现:
第一种 :
FirefoxOptions option=new FirefoxOptions(); option.addPreference("zoom.maxPercent",130);第二种
option.configureFromEnv().addPreference("zoom.maxPercent",130);全部的代码:
package net.highersoft.service; import lombok.extern.slf4j.Slf4j; import net.highersoft.entity.SupObject; import net.highersoft.util.JDBCUtil; import org.jsoup.Jsoup; import org.openqa.selenium.By; import org.openqa.selenium.TimeoutException; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.firefox.FirefoxOptions; import org.openqa.selenium.firefox.FirefoxProfile; import org.openqa.selenium.firefox.GeckoDriverService; import org.openqa.selenium.remote.RemoteWebDriver; import org.openqa.selenium.remote.http.ClientConfig; import java.io.File; import java.net.URL; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; import java.time.Duration; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j public class TestSingleURL { private static final int timeoutSec=60; public static void main(String args[]) throws Exception{ log.info("start"); String url = "http://www.highersoft.net"; //String url = "about:config"; // 设置系统属性以指定GeckoDriver的位置 //System.setProperty("webdriver.gecko.driver","/Users/chengzhong/code/linking_workspace/selenium_browser2/geckodriver_mac"); System.setProperty("webdriver.gecko.driver","C:\\Users\\koolf\\java_project\\selenium_browser2\\geckodriver_win64.exe"); FirefoxOptions option=new FirefoxOptions(); /* option.addPreference("network.dns.disableIPv4","true"); option.addPreference("network.dns.disableIPv6","false"); option.addPreference("zoom.maxPercent",130); */ FirefoxDriver driver=getWebDriver(option); parseUrl(url, driver); Object code = driver.executeScript("return window.performance.getEntries()[0].responseStatus"); String errMsg=""; String txt = Jsoup.parse(driver.getPageSource()).text(); if (code != null&&!String.valueOf(code).equals("0")&&!String.valueOf(code).equals("200")){ //System.out.println(i + " url:" + url + " response code:" + code); } else { if (txt.length() > 200) { errMsg=txt.substring(0, 200); } else { errMsg=txt; } } String codeMsg=""; System.out.println(codeMsg+" url:"+url+" txt:"+txt); //driver.close(); log.info("end"); } public static List<SupObject> parseUrl(String url, WebDriver driver) { List<SupObject> urlList=new ArrayList<>(); try { URL mainUrl=new URL(url); try { driver.get(url); }catch(TimeoutException e1){ //加载超时,这时也能找到A标签 log.error(e1.getMessage()); }catch(Exception e){ log.error(e.getMessage(),e); return urlList; } }catch (Exception e){ e.printStackTrace(); } return urlList; } /* 获取驱动对象 */ private static FirefoxDriver getWebDriver(FirefoxOptions option) throws Exception{ Map<String, String> map = new HashMap<String, String>(); map.put("MOZ_LOG", "timestamp,sync,nsHttp:4"); String filePath="/Users/chengzhong/code/idea_workspace/ipv6/log/firefox.log"; File tempFile = new File(filePath); map.put("MOZ_LOG_FILE", tempFile.getAbsolutePath()); GeckoDriverService.Builder builder = new GeckoDriverService.Builder(); final GeckoDriverService service = builder.usingAnyFreePort() .withEnvironment(map).withTimeout(Duration.ofSeconds(3)) //.withLogFile(new File(tempFile.getAbsolutePath())) .build(); service.start(); // ClientConfig clientConfig=ClientConfig.defaultConfig(); //多次driver.get不报错, option.setCapability("unhandledPromptBehavior","dismiss"); //设置n秒,加载不出页面就退出 option.setPageLoadTimeout(Duration.ofSeconds(timeoutSec)); //ImplicitWaitTimeout方法用于设置隐式等待的超时时间。隐式等待是指在每次执行查找元素的操作前,WebDriver都会等待一段时间,以确保所需元素已经加载到页面上。 option.setImplicitWaitTimeout(Duration.ofSeconds(timeoutSec)); // // /* option.configureFromEnv().addPreference("network.dnsCacheExpiration",0); option.configureFromEnv().addPreference("network.http.use-cache",false); option.configureFromEnv().addPreference("network.dns.disableIPv4",true); option.configureFromEnv().addPreference("network.dns.disableIPv6",false); option.configureFromEnv().addPreference("network.http.fast-fallback-to-IPv4",false); */ option.configureFromEnv().addPreference("zoom.maxPercent",130); FirefoxDriver driver = new FirefoxDriver(service,option); return driver; } }
相关阅读
评论:
↓ 广告开始-头部带绿为生活 ↓
↑ 广告结束-尾部支持多点击 ↑