以下代码的功能如下:
下面是代码。
import java.util.*;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class downloaddemo {
String text ;
String text1 ;
WebDriver driver;
void getTextU()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter Text");
text = s.nextLine();
text1 = text + " pdf " ;
System.out.println(" Searching for " + text1 + " .......... ");
}
public void invokeBrowser()
{
System.setProperty("webdriver.gecko.driver","C:\\geckodriver-v0.19.0-win64\\geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS );
driver.get("https://www.google.com");
search();
}
public void search()
{
driver.findElement(By.xpath("//input[@id='lst-ib' and @class='gsfi']")).click();
System.out.println(text1);
driver.findElement(By.id("lst-ib")).sendKeys(text1);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
driver.findElement(By.xpath("//input[@value='Google Search' or @aria-label='Google Search']")).click();
//driver.findElement(By.xpath("//span[@class='_ogd b w xsm'] and //a[@href='']")).click();
/*List<WebElement> list = driver.findElements(By.xpath("//span[@class='_ogd b w xsm']//a[@href]"));
for (WebElement e : list) {
String link = e.getAttribute("href");
System.out.println(e.getTagName() + "=" + link + " , " + e.getText());
}*/
}
public static void main(String[] args) {
// TODO Auto-generated method stub
downloaddemo d = new downloaddemo();
d.getTextU();
d.invokeBrowser();
}
}
它显示输出如下:
问题是我如何加载任何一个pdf链接,问题是每个锚标签都有不同的href值。
或
我该如何下载所有pdf。
答案 0 :(得分:1)
使用此代码我将获取所有pdf链接
public class testing_solution
{
public static WebDriver driver;
public static void main(String[] args) throws InterruptedException {
driver =new ChromeDriver();
driver.get("https://www.google.com/search?q=selenium+pdf&rlz=1C1CHZL_enBD739BD739&oq=selenium+pdf&aqs=chrome..69i57j69i60l2j0l3.3815j0j7&sourceid=chrome&ie=UTF-8");
List<WebElement> list = driver.findElements(By.xpath(".//*[@class='f kv _SWb']/cite"));
for (WebElement e : list) {
if(e.getText().endsWith(".pdf")){
System.out.println(e.getText());
}
}}
首先,我获取链接的链接,然后检查链接是否为pdf,然后打印所有链接。
我得到以下输出
希望它能帮到你......