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);

最新内容请见作者的GitHub页:http://qaseven.github.io/
相关文章
|
4月前
|
Web App开发 Python
python+selenium+Chrome 监测文件是否下载完毕
python+selenium+Chrome 监测文件是否下载完毕
74 0
|
3月前
|
Web App开发 IDE 测试技术
实战练习:用airtest-selenium脚本爬取百度热搜标题
实战练习:用airtest-selenium脚本爬取百度热搜标题
|
4月前
|
API Python
python+selenium设置下载路径
python+selenium设置下载路径
33 0
|
4月前
|
API Python
python+selenium设置下载路径21 / 100
python+selenium设置下载路径21 / 100
31 0
|
5月前
|
Linux
Linux上传下载百度网盘文件
Linux上传下载百度网盘文件
101 0
|
5月前
设置selenium默认下载位置
设置selenium默认下载位置
107 1
|
5月前
|
Shell Windows
Windows【工具 03】QuickLook-3.6.11安装并设置开机自启动(含较难下载的 QuickLook-3.6.11 安装包百度云盘资源)
Windows【工具 03】QuickLook-3.6.11安装并设置开机自启动(含较难下载的 QuickLook-3.6.11 安装包百度云盘资源)
64 0
|
5月前
|
Web App开发 Python Windows
Python Selenium 驱动下载与配置使用(详细流程)
Python Selenium 驱动下载与配置使用(详细流程)
140 0
|
11月前
|
Web App开发 Python
Selenium Python 更改 chrome 默认下载目录
关于使用Selenium和Python无法更改Google Chrome默认下载目录的可能问题和解决方法,按照以上步骤,你应该能够成功使用Selenium和Python更改Google Chrome的默认下载目录。
240 0
|
9月前
|
Python
wallhaven壁纸下载-selenium版本
wallhaven壁纸下载-selenium版本
85 0