selenium+java-查找页面中包含关键字的URL

package seleniumLearn1;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List; import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver; import com.gargoylesoftware.htmlunit.javascript.host.Element; /**
* 获取http://www.qyer.com页面中,所有</a>标签"href"属性值
* 包含英文单词“place”的URL,并将结果保存到“/home/result.log”文件中。
* @author 0
*
*/
public class Search { static String baseUrl="http://www.qyer.com";
public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver", "E:\\\\webDriver\\\\chromedriverV2.28.exe");
File logFile = new File("d://logFile.txt");
if(!(logFile.exists())) {
try {
logFile.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} WebDriver driver = new ChromeDriver();
driver.get(baseUrl); //获取所有a标签
List<WebElement> aList = driver.findElements(By.tagName("a"));
try {
Thread.sleep(10000);
} catch (Exception e) {
e.printStackTrace();
}
//便利所有标签
FileOutputStream fs = null;
try {
fs = new FileOutputStream(logFile);
for (WebElement a : aList) {
System.out.println(a.getAttribute("href"));//获取a标签中的URL //获取a标签href属性值
String urlStr = a.getAttribute("href");
if(urlStr.contains("place")) {
urlStr +="\r\n";
//将URL写入文件中
fs.write(urlStr.getBytes());
} }
} catch (Exception e) {
e.printStackTrace();
}
}
}

  

上一篇:iOS开发——动画篇Swift篇&炫酷弹出菜单


下一篇:【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类