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 { System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\#Downloads\\chromedriver_win32\\chromedriver.exe"); ChromeDriver driver = new ChromeDriver();
driver.get("https://koding.com/Login");
WebElement element = driver.findElement(By.name("username"));
CharSequence[] ch = new CharSequence[] { "xxx" }; element.sendKeys(ch);
WebElement element2 = driver.findElement(By.name("password")); element2.sendKeys(new CharSequence[] { "vvv" });
element.submit();
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());
driver.quit(); } }
|