我正在使用 Selenium 编写一个需要多线程的网络爬虫// selenium imports from org.openqa omittedpublic class wpn{ private String sessionId = ""; private String windowHandle = ""; private WebDriver driver; public wpn (String id) { sessionId = id; } public void openDriver (String driverType) { if (driverType.equals ("ie")) { DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer(); capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true); capabilities.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true); capabilities.setCapability("requireWindowFocus", true); System.setProperty("webdriver.ie.driver", "c:\\temp\\ieDriver.exe"); driver = new InternetExplorerDriver(capabilities); driver.manage().window().maximize(); } else if (driverType.equals ("chrome")) { DesiredCapabilities capabilities = DesiredCapabilities.chrome(); capabilities.setCapability(CapabilityType.ForSeleniumServer.ENSURING_CLEAN_SESSION, true); System.setProperty("webdriver.chrome.driver", "c:\\temp\\chromedriver.exe"); driver = new ChromeDriver (capabilities); driver.manage().window().maximize(); } else if (driverType.equals ("phantom")) { File file = new File("c:\\temp\\phantomJSDdriver.exe"); System.setProperty("phantomjs.binary.path", file.getAbsolutePath ()); DesiredCapabilities capabilities = new DesiredCapabilities(); capabilities.setJavascriptEnabled(true); capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, new String[] {"--web-security=no", "--ignore-ssl-errors=yes"}); driver = new PhantomJSDriver(capabilities); Dimension dimension = new Dimension (1280, 1024); driver.manage().window().setSize (dimension); }
2 回答
噜噜哒
TA贡献1784条经验 获得超7个赞
您正在使用匹配,这是一个正则表达式匹配器。driver.getWindowHandle
返回一个字符串,您应该使用它equals
来检查相等性。此外,由于您没有在 之后session.login(...)
和之前切换窗口session.info(...)
,因此检查驱动程序是否指向同一个窗口是多余的。
您已经初始化了两个独立的驱动程序实例,它们引用了它们自己的浏览器窗口。驱动它们自己的浏览器会话的驱动程序实例之间没有“串扰”。
如果您仍想检查是否仍指向同一个窗口,请使用wh.equals(windowHandle)
.
添加回答
举报
0/150
提交
取消