- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
driver_fire.manage().timeouts().implicitlyWait(4, TimeUnit.SECONDS);
wait=new WebDriverWait(driver_fire,10);
driver_fire.navigate().to("http://www.mysite.com");
LoginForm2 loginForm2 = PageFactory.initElements(driver_fire, LoginForm2.class);
loginForm2.logIn("login", "password");
wait.until((WebDriver d)->d.manage().getCookieNamed("user_id")).getValue(); // if we have this cookie, we have a logged in session
// реализация класса формы логина
public class LoginForm2 {
@FindBy(css="div.log-in#log-in")
private WebElement loginForm;
@FindBy (css=".login")
private WebElement invoke_button;
WebDriverWait wait;
WebDriver driver;
public LoginForm2(WebDriver driver){
driver.manage().timeouts().implicitlyWait(0,TimeUnit.SECONDS);
this.driver = driver;
wait = new WebDriverWait(driver,60);
}
private boolean checkCaptha() {
try {
WebElement captcha = loginForm.findElement(By.cssSelector("#newLoginForm iframe"));
driver.switchTo().frame(captcha);
try {
wait.until(ExpectedConditions.attributeToBe(By.id("recaptcha-anchor"),"aria-checked","true"));
System.out.println("Passed captcha");
driver.switchTo().defaultContent();
return true;
} catch (TimeoutException e) {
System.out.println("Too long to wait for captcha"); return false;
}
} catch (NoSuchElementException e) {
System.out.println("No captcha )"); return true;
}
}
public void logIn(String email, String password)
{
wait.until(ExpectedConditions.visibilityOf(invoke_button)).click();
wait.until(ExpectedConditions.visibilityOf(loginForm));
loginForm.findElement(By.name("name")).sendKeys(email);
loginForm.findElement(By.name("passwd")).sendKeys(password);
if( checkCaptha()) loginForm.findElement(By.name("login-button")).click();
}
}
Selenium: Логин на сайте с задержкой на прохождение Google reCaptcha. Цель - только получить залогиненную сессию.
Комментарии (0) RSS
Добавить комментарий