Selenium+JAVA淘宝登录、商品搜索、添加收货地址,提交订单、付款流程
package test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.ArrayList;
import java.util.List;
public class UITaobao {
public static void main(String[] args) throws Exception {
String url = "https://www.taobao.com/";
allProcess(url);
}
public static void allProcess(String url) throws Exception {
System.setProperty("webdirver.chrome.dirver", "D:\\Tools\\webdirveri\\chromedriver.exe");
ChromeDriver chromeDriver = new ChromeDriver();
chromeDriver.get(url);
chromeDriver.findElementByXPath("//*[@id=\"J_LoginInfoHd\"]/a[1]");
WebDriverWait wait = new WebDriverWait(chromeDriver, 10);
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"login-form\"]/div[4]/button")));
chromeDriver.findElementByXPath("//*[@id=\"fm-login-id\"]").clear();
chromeDriver.findElementByXPath("//*[@id=\"fm-login-id\"]").sendKeys("wwww");
chromeDriver.findElementByXPath("//*[@id=\"fm-login-password\"]").clear();
chromeDriver.findElementByXPath("//*[@id=\"fm-login-password\"]").sendKeys("12345");
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"J_SiteNavBdR\"]/li[1]/div/a")));
chromeDriver.findElementByXPath("//*[@id=\"J_search_key\"]").sendKeys("水杯");
chromeDriver.findElementByXPath("//*[@id=\"J_searchForm\"]/input").click();
String nowWindows = chromeDriver.getWindowHandle();
List<String> allWindows = new ArrayList<>();
int j = 0;
for (int i = 0; i < allWindows.size(); i++) {
if (allWindows.get(i).equals(nowWindows)) {
j = i;
break;
}
}
chromeDriver.switchTo().window(allWindows.get(j));
chromeDriver.findElementByXPath("//*[@id=\"mx_5\"]/ul/li[1]/a/div[1]/span").click();
Thread.sleep(2);
for (int i = 0; i < allWindows.size(); i++) {
if (allWindows.get(i).equals(nowWindows)) {
j = i;
break;
}
}
chromeDriver.switchTo().window(allWindows.get(j));
chromeDriver.findElementByXPath("//*[@id=\"mx_9\"]/div/div/a/div[3]/p").click();
Thread.sleep(2);
chromeDriver.findElementByXPath("//*[@id=\"J_DetailMeta\"]/div[1]/div[1]/div/div[4]/div/div/dl[1]/dd/ul/li[1]/a/span")
.click();
chromeDriver.findElementByXPath("//*[@id=\"J_Amount\"]/span[1]/input").clear();
chromeDriver.findElementByXPath("//*[@id=\"J_Amount\"]/span[1]/input").sendKeys("2");
chromeDriver.findElementByXPath("//*[@id=\"J_LinkBuy\"]/span").click();
chromeDriver.findElementByXPath("//*[@id=\"addressPC_1\"]/div[5]/button").click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"dialog-title-kt1lt9ab\"]")));
chromeDriver.findElementByCssSelector
("#cndzkEntrance > div:nth-child(2) > div > div > div.cndzk-entrance-division-box > ul.cndzk-entrance-division-box-content > div > li:nth-child(1)")
.click();
chromeDriver.findElementByCssSelector
("#cndzkEntrance > div:nth-child(2) > div > div > div.cndzk-entrance-division-box > ul.cndzk-entrance-division-box-content > div > li:nth-child(1)")
.click();
chromeDriver.findElementByCssSelector("#cndzkEntrance > div:nth-child(2) > div > div > div.cndzk-entrance-division-box > ul.cndzk-entrance-division-box-content > div > li:nth-child(1)")
.click();
chromeDriver.findElementByCssSelector("#cndzkEntrance > div:nth-child(2) > div > div > div.cndzk-entrance-division-box > ul.cndzk-entrance-division-box-content > div > li:nth-child(1)")
.click();
chromeDriver.findElementByXPath("//*[@id=\"cndzkEntrance\"]/div[4]/div/div/textarea").clear();
chromeDriver.findElementByXPath("//*[@id=\"cndzkEntrance\"]/div[4]/div/div/textarea").sendKeys("xxxxx");
chromeDriver.findElementByXPath("//*[@id=\"cndzkEntrance\"]/div[4]/div/div/textarea").clear();
chromeDriver.findElementByXPath("//*[@id=\"cndzkEntrance\"]/div[4]/div/div/textarea").sendKeys("XXX");
chromeDriver.findElementByXPath("//*[@id=\"mobile\"]").clear();
chromeDriver.findElementByXPath("//*[@id=\"mobile\"]").sendKeys("1903083");
chromeDriver.findElementByXPath("//*[@id=\"myForm\"]/div[5]/div[2]/button/span").click();
chromeDriver.findElementByXPath("//*[@id=\"addressPC_1\"]/div[3]/div[1]/div[1]").click();
chromeDriver.findElementByCssSelector("#submitOrderPC_1 > div > a").click();
wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.xpath("//*[@id=\"header\"]/div/div[1]/span[1]/s")));
WebElement alipayPasswdFream = chromeDriver.findElementByXPath("//*[@id=\"payPassword_container\"]/div");
List<WebElement> ilist = alipayPasswdFream.findElements(By.tagName("i"));
for (int i = 0; i < ilist.size(); i++) {
WebElement iElement = ilist.get(i);
iElement.sendKeys("1");
}
chromeDriver.findElementByXPath("//*[@id=\"J_authSubmit\"]").click();
chromeDriver.close();
}
}
|