package com.cn.selenium;
import java.util.concurrent.TimeUnit; import org.junit.*;
import static org.junit.Assert.*; import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
public class First {
private WebDriver driver; private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
@Before
public void setUp() throws Exception {
System.setProperty(\"webdriver.chrome.driver\ driver = new ChromeDriver();
baseUrl = \"http://www.baidu.com/\";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); }
@Test
public void testFirst() throws Exception { driver.get(baseUrl + \"/\");
driver.findElement(By.id(\"kw\")).click(); driver.findElement(By.id(\"kw\")).clear();
driver.findElement(By.id(\"kw\")).sendKeys(\"天气预报\"); driver.findElement(By.id(\"su\")).click();
driver.findElement(By.xpath(\"//table[@id='1']/tbody/tr/td/h3/a/em[2]\")).click(); }
@After
public void tearDown() throws Exception { driver.quit();
String verificationErrorString = verificationErrors.toString(); if (!\"\".equals(verificationErrorString)) { fail(verificationErrorString); } }
private boolean isElementPresent(By by) { try {
driver.findElement(by); return true;
} catch (NoSuchElementException e) { return false; } }
private boolean isAlertPresent() { try {
driver.switchTo().alert(); return true;
} catch (NoAlertPresentException e) { return false; } }
private String closeAlertAndGetItsText() { try {
Alert alert = driver.switchTo().alert(); String alertText = alert.getText(); if (acceptNextAlert) { alert.accept(); } else {
alert.dismiss(); }
return alertText; } finally {
acceptNextAlert = true; } } }
# -*- coding: UTF-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException import unittest, time, re import os
class Pythontest(unittest.TestCase):
def setUp(self):
chromedriver = \"D:\\chromedriver_win32\\chromedriver.exe\" os.environ[\"webdriver.chrome.driver\"] = chromedriver self.driver = webdriver.Chrome(chromedriver)
self.driver.implicitly_wait(30)#智能等待30秒 self.base_url = \"http://www.baidu.com\" self.verificationErrors = [] self.accept_next_alert = True
def test_python(self): driver = self.driver
driver.get(self.base_url + \"/\")
# driver.find_element_by_name(\"wd\").click() # print(\"1\")
# driver.find_element_by_name(\"wd\").clear() # print(\"2\")
driver.find_element_by_name(\"wd\").send_keys(u\"selenuim\") print(\"3\")
driver.find_element_by_id(\"su\").click() print(\"4\")
driver.find_element_by_link_text(\"朋友网\").click()
def is_element_present(self, how, what):
try: self.driver.find_element(by=how, value=what) except NoSuchElementException, e: return False return True
def is_alert_present(self):
try: self.driver.switch_to_alert()
except NoAlertPresentException, e: return False return True
def close_alert_and_get_its_text(self): try:
alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else:
alert.dismiss() return alert_text
finally: self.accept_next_alert = True
def tearDown(self): self.driver.quit()
self.assertEqual([], self.verificationErrors)
if __name__ == \"__main__\": unittest.main() package com.cn.selenium; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example {
public static void main(String[] args) { //创建一个WebDriver实例 System.setProperty(\"webdriver.chrome.driver\;
WebDriver driver= new ChromeDriver(); // 访问google
driver.get(\"http://www.baidu.com\");
// 另一种访问方法
// driver.navigate().to(\"http://www.google.com\");
// 找到文本框
WebElement element= driver.findElement(By.name(\"wd\")); // 输入搜索关键字
element.sendKeys(\"Selenium\");
//提交表单 WebDriver会自动从表单中查找提交按钮并提交 element.submit(); // 检查页面title
System.out.println(\"Page title is: \" + driver.getTitle());
// google查询结果是通过javascript动态呈现的. // 设置页面等待10秒超时
(new WebDriverWait(driver, 10)).until(new ExpectedCondition() { public Boolean apply(WebDriver d) {return d.getTitle().toLowerCase().startsWith(\"Selenium\"); } });
// 显示查询结果title
System.out.println(\"Page title is: \" + driver.getTitle());
//关闭浏览器 driver.quit(); } }