Auto login in koding.com using selenium

1
2
3
4
5
6
7
8
9
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package xx;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.google.common.base.Function;

public class Selenium2Example {
public static void main(String[] args) throws MalformedURLException {
// Create a new instance of the Chrome driver
// Notice that the remainder of the code relies on the interface,
// not the implementation.
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\User\\#Downloads\\chromedriver_win32\\chromedriver.exe");
ChromeDriver driver = new ChromeDriver();

//WebDriver driver = new RemoteWebDriver(new URL("http://127.0.0.1:9515"), DesiredCapabilities.chrome());

// And now use this to visit Google
driver.get("https://koding.com/Login");
// Alternatively the same thing can be done like this
// driver.navigate().to("http://www.google.com");

// Find the text input element by its name
WebElement element = driver.findElement(By.name("username"));

// Enter something to search for
CharSequence[] ch = new CharSequence[] { "xxx" };
element.sendKeys(ch);

WebElement element2 = driver.findElement(By.name("password"));
element2.sendKeys(new CharSequence[] { "vvv" });

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());

try {
WebElement button = (new WebDriverWait(driver, 20)).until(new Function<WebDriver, WebElement>() {
public WebElement apply(WebDriver driver) {
List<WebElement> els = driver.findElements(By.id("kd-376"));
if (!els.isEmpty()) {
return els.get(0);
}
return null;
}
});

if (button != null) {
button.click();
new WebDriverWait(driver, 10);
}
} catch (Exception e) {
e.printStackTrace();
}

System.out.println("Page title is: " + driver.getTitle());

//Close the browser
driver.quit();
}
}