#导包
from selenium import webdriver
from time import sleep
#创建一个驱动对象
browser = webdriver.Chrome()
#使用谷歌浏览器驱动打开浏览器飞猪网主页
browser.get('https://www.fliggy.com/')
#通过名字定位出发城市填写框
DepCityName = browser.find_element_by_name('depCityName')
#清空出发城市
DepCityName.clean()
#出发城市填入北京
DepCityName.send_keys('北京')
#模拟人休息2秒
time.sleep(2)
#通过名字定位出发时间填写框
DepDate = brower.find_element_by_name('depDate')
#填写框填写2021-7-9
DepDate.send_keys('2021-7-9')
#模拟人休息2秒
time.sleep(3)
#点击关闭日期控件
browser.find_element_by_Xpath('//span[text()='close']').click()
#模拟人休息2秒
time.sleep(2)
#通过名字定位到达城市填写框
ArrCityName = browser.find_element_by_name('arrCityName')
#到达城市填入长沙
ArrCityName.send_keys('长沙')
#模拟人休息2秒
time.sleep(2)
#通过名字定位找到搜索按钮
SearchButtom = browser.find_element_by_Xpath('//form[@id='J_FlightForm']//button')
#点击搜索按钮
SearchButtom.click()
#模拟人休息2秒
time.sleep(2)
browser.quit()
package com.demo;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.Set;
public class demo {
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.baidu.com/");
driver.findElementById("kw").sendKeys("测试派");
driver.findElementById("su").click();
Thread.sleep(1000);
driver.findElementByPartialLinkText("软件测试工程师的心灵社区").click();
Thread.sleep(1000);
String search_handle = driver.getWindowHandle();
Set<String> handles = driver.getWindowHandles();
for (String handle : handles) {
if (!handle.equals(search_handle)) {
driver.switchTo().window(handle);
Thread.sleep(1000);
driver.findElementById("navLogin").click();
Thread.sleep(1000);
driver.findElementById("nameOrEmail").sendKeys("ke2beck");
driver.findElementById("loginPassword").sendKeys("********");
driver.findElementById("loginBtn").click();
Thread.sleep(1000);
driver.findElementByXPath("/html/body/header/nav/a[2]").click();
Thread.sleep(5000);
driver.close();
}
}
driver.quit();
}
}
```
```
package com.demo;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class fliggyDemo {
public static void inputElement(ChromeDriver driver, String eleName, String text) {
WebElement element = driver.findElementByName(eleName);
element.clear();
element.sendKeys(text);
}
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "src/test/resources/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.fliggy.com/");
Thread.sleep(2000);
inputElement(driver,"depCityName","北京");
inputElement(driver,"depDate","2021-07-10");
driver.findElementByXPath("//span[text()='close']").click();
inputElement(driver,"arrCityName","长沙");
driver.findElementByXPath("//form[@id='J_FlightForm']//button").click();
Thread.sleep(5000);
driver.quit();
}
}
欢迎来到testingpai.com!
注册 关于