Java 利用selenium登录大众点评团购信息(谷歌浏览器)

配置代码见之前一篇文章

戳我

public static List> loginDianPingWUP(WebDriver webDriver, String username, String password) {
		webDriver.get(SysConstants.SysConfig.DIANPINGLOGINURL);
		// 显示等待控制对象
		try {
			Thread.sleep(2000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		WebDriverWait webDriverWait = new WebDriverWait(webDriver, 5);
		WebElement element = webDriverWait.until(ExpectedConditions.elementToBeClickable(By.tagName("iframe")));
		WebDriver frame = webDriver.switchTo().frame(element);
		webDriverWait = new WebDriverWait(frame, 5);

		webDriverWait.until(ExpectedConditions.elementToBeClickable(By.className("bottom-password-login"))).click();
		webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("tab-account"))).click();
		webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("account-textbox"))).sendKeys(username);
		webDriverWait.until(ExpectedConditions.elementToBeClickable(By.id("password-textbox"))).sendKeys(password);
		webDriver.findElement(By.id("login-button-account")).click();

		// 等待2秒用于页面加载,保证Cookie响应全部获取。
		try {
			while (true) {
				Thread.sleep(2000L);
				if (!webDriver.getCurrentUrl().startsWith(SysConstants.SysConfig.DIANPINGLOGINURL)) {
					break;
				}
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		Set cookies = webDriver.manage().getCookies();
		List> list = new ArrayList>();
		for (Cookie cookie : cookies) {
			list.add(cookie.toJson());
		}
		return list;
	}

这段代码将之前停顿20秒来扫码登录的过程可以修改

然后缺少验证拖拽的逻辑

你可能感兴趣的:(JAVA)