Selenium下载百度音乐并验证
package baidu;
import
java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
//import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
public class selenium {
public static void snapshot(TakesScreenshot drivername, String filename)
{
// this method will take screen shot ,require two parameters ,one is driver name, another is file name
File scrFile = drivername.getScreenshotAs(OutputType.FILE);
// Now you can do whatever you need to do with it, for example copy somewhere
try {
System.out.println("save snapshot path is:E:/"+filename);
FileUtils.copyFile(scrFile, new File("E:\\"+filename));
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("Can't save screenshot");
e.printStackTrace();
}
finally
{
System.out.println("screen shot finished");
}
}
public static void main (String [] args) throws InterruptedException
{
String URL="http://61.135.169.105/";
//avoid Chrome warnning message like "unsupported command-line flag --ignore-certificate-errors. "
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
driver.get(URL);
//max size the browser
driver.manage().window().maximize();
/*
Navigation navigation = driver.navigate();
navigation.to(URL);*/
Thread.sleep(2000);
snapshot((TakesScreenshot)driver,"open_baidu.png");
//WebElement reg=driver.findElement(By.name("tj_reg"));
//reg.click();
// WebElement keyWord = driver.findElement(By.id("kw1"));
//find the element
WebElement keyWord = driver.findElement(By.xpath("//input[@id='kw1']"));
keyWord.clear();
//send key words
keyWord.sendKeys("小
苹果");
Thread.sleep(3000);
snapshot((TakesScreenshot)driver,"input_keyWord.png");
WebElement submit = driver.findElement(By.id("su1"));
System.out.println(submit.getLocation());
submit.click();
//System.out.println(driver.getWindowHandle());
Thread.sleep(5000);
WebElement se=driver.findElement(By.xpath("//*[@id=\"2\"]/div[1]/div[2]/table/tbody/tr/td[5]/span/a")) ;
Actions action = new Actions(driver);
action.clickAndHold(se);
action.sendKeys(Keys.DOWN);
Thread.sleep(5000);
List<WebElement> elementList = driver.findElements(By.tagName("herf"));
for(WebElement e:elementList)
{
System.out.print("-->"+e.getText());
}
//se.click();
// System.out.println(driver.getPageSource());
// System.out.println(pageSource);
//WebElement link =driver.findElement(By.xpath(SELENIUM_LINK));
WebElement link =driver.findElement(By.xpath("//*[@id=\"2\"]/div[1]/div[2]/table/tbody/tr/td[5]/span/a"));//By.xpath("//*[@id=\"1\"]/h3/a")); //*[@id="1"]/h3/a
link.click();
Thread.sleep(5000);
driver.switchTo().window(driver.getWindowHandles().toArray(new String[0])[1]);
Thread.sleep(5000);
WebElement down =driver.findElement(By.xpath("//*[@id=\"128\"]"));//<i class="icon btn-icon-download-small"></i>
Thread.sleep(5000);
down.click();
snapshot((TakesScreenshot)driver,"down_m.png");
//get page title
System.out.println(driver.getTitle());
Thread.sleep(5000);
WebElement userName=driver.findElement(By.id("TANGRAM__PSP_8__userName"));
WebElement password=driver.findElement(By.id("TANGRAM__PSP_8__password"));
WebElement login=driver.findElement(By.id("TANGRAM__PSP_8__submit"));
Thread.sleep(5000);
userName.sendKeys("QAtest");//your baidu userName
password.sendKeys("mypassword");//your baidu password
login.submit();
// navigation.back();
snapshot((TakesScreenshot)driver,"open_bake.png");
System.out.println(driver.getTitle()+"\n"+driver.getCurrentUrl());
Thread.sleep(50000);
File file=new File("C:\\Users\\Young\\Downloads\\小苹果.mp3");
if(file.exists())
{
System.out.println("PASS");
}
else
{
System.out.println("FAIL");
}
driver.quit();
}
}
|
如果出现:unsupported command-line flag --ignore-certificate-errors.
//avoid Chrome warnning message like "unsupported command-line flag --ignore-certificate-errors. "
ChromeOptions options = new ChromeOptions();
options.addArguments("--test-type");
System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver.exe");
WebDriver driver = new ChromeDriver(options);
English »
|
AfrikaansAlbanianArabicArmenianAzerbaijaniBasqueBengaliBelarusianBulgarianCatalanChinese (Simp)Chinese (Trad)CroatianCzechDanishDutchEnglishEsperantoEstonianFilipinoFinnishFrenchGalicianGeorgianGermanGreekGujaratiHaitian CreoleHebrewHindiHungarianIcelandicIndonesianIrishItalianJapaneseKannadaKoreanLaoLatinLatvianLithuanianMacedonianMalayMalteseNorwegianPersianPolishPortugueseRomanianRussianSerbianSlovakSlovenianSpanishSwahiliSwedishTamilTeluguThaiTurkishUkrainianUrduVietnameseWelshYiddish |
Options : History : Help : Feedback