Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在Selenium中打印Google.com前5页上所有结果链接的名称_Selenium - Fatal编程技术网

无法在Selenium中打印Google.com前5页上所有结果链接的名称

无法在Selenium中打印Google.com前5页上所有结果链接的名称,selenium,Selenium,我试图在Google.com的前5页打印所有结果链接的名称 情况是, 1) 去www.google.com搜索一些东西。 2) 打印前5页上所有结果链接的名称 我可以打印第一页的所有结果链接,然后单击下一页链接。但是第二页链接没有打印。我认为原因可能是第二页中xpath发生了变化。如果是,我如何打印第2页和第5页的所有链接。 请帮我解决这个问题 package com; import java.util.List; import java.util.concurrent.TimeUnit;

我试图在Google.com的前5页打印所有结果链接的名称

情况是, 1) 去www.google.com搜索一些东西。 2) 打印前5页上所有结果链接的名称

我可以打印第一页的所有结果链接,然后单击下一页链接。但是第二页链接没有打印。我认为原因可能是第二页中xpath发生了变化。如果是,我如何打印第2页和第5页的所有链接。 请帮我解决这个问题

package com;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class Exercise2 {
static WebDriver d;
    public static void main(String[] args) throws InterruptedException {
        ProfilesIni prop = new ProfilesIni();
        FirefoxProfile seleniumProfile = prop.getProfile(("Selenium"));
         d = new FirefoxDriver();
        d.manage().window().maximize();
        d.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

        d.get("http://www.google.com");
        Thread.sleep(5000);
        d.findElement(By.name("q")).sendKeys("Selenium");
        d.findElement(By.name("q")).sendKeys(Keys.ENTER);

        Thread.sleep(5000);

        //Print names of all result links on first page.
        String part1 = "//div[@id='rso']/div[2]/div[";
        String part2  = "]/div/h3/a";

        for(int i=2;i<=5;i++){
            String part3 = "//div[@id='navcnt']/table/tbody/tr/td[";
            String part4 = "]";
            int a = 1;

            while(isElementPresent(part1+a+part2)){
                String text = d.findElement(By.xpath(part1+a+part2)).getText();
                System.out.println(text);
                a++;
            }
            Thread.sleep(5000);
            System.out.println("*************Next Page**********");
            Thread.sleep(5000);
            d.findElement(By.xpath(part3+i+part4)).click();
        }

}
    public static boolean isElementPresent(String xpathexp){
        List<WebElement> allList = d.findElements(By.xpath(xpathexp));
        //WebElement nextPages = d.findElement(By.xpath(nextPage1));
        if(allList.size()==0)
        {
            return false;
        }else{
            return true;
        }

    }

}
package-com;
导入java.util.List;
导入java.util.concurrent.TimeUnit;
导入org.openqa.selenium.By;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebDriver;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
导入org.openqa.selenium.firefox.FirefoxProfile;
导入org.openqa.selenium.firefox.internal.ProfilesIni;
公开课练习2{
静态web驱动程序d;
公共静态void main(字符串[]args)引发InterruptedException{
ProfilesIni prop=新ProfilesIni();
FirefoxProfile seleniumProfile=prop.getProfile((“Selenium”);
d=新的FirefoxDriver();
d、 管理().window().maximize();
d、 manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
d、 得到(”http://www.google.com");
睡眠(5000);
d、 findElement(按名称(“q”)).sendKeys(“硒”);
d、 findElement(By.name(“q”)).sendKeys(key.ENTER);
睡眠(5000);
//打印第一页上所有结果链接的名称。
字符串part1=“//div[@id='rso']/div[2]/div[”;
字符串part2=“]/div/h3/a”;
对于(int i=2;i
List links=driver.findElements(按.tagName(“a”));
对于(int i=0;i<5;i++)
{
字符串firstLink=links[i].getAttribute(“href”);
}
尝试以下代码

package javaselenium;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
 *
 * @author Muhammad USman
 */
public class JavaSelenium {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Hello Wrold");
    GetUrls("Selenium");
}   

public static  ArrayList GetUrls(String keyWord)
{
    FirefoxDriver driver;        
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("https://www.google.com/?gws_rd=ssl"); 
    WebElement qElement;
    qElement = driver.findElement(By.name("q"));
    qElement.sendKeys(keyWord);
    qElement.sendKeys(Keys.ENTER);
    try {
        Thread.sleep(3*1000);
    } catch (InterruptedException ex) {
        Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
    }
    //give number how mange pages you want to crawl
    int nPageToCrawl=5;
    List links;
    links=new ArrayList();
    for (int i = 0; i < nPageToCrawl; i++) {            
        if(i>0)
        {
            System.out.println("***Click on Next Page***");
            //Click on Next Page
            driver.findElement(By.id("pnnext")).click();
            try {
                Thread.sleep(3*1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //it will get all links of pageLinks
        List<WebElement>  pageLinks = driver.findElements(By.cssSelector(".r>a"));
        for (WebElement pageLink : pageLinks) {
            links.add(pageLink.getAttribute("href"));
            System.out.println(pageLink.getText());
            System.out.println(pageLink.getAttribute("href"));
        }
    }
    //close browser
    driver.quit();
    return (ArrayList) links;
}
包javaselenium;
导入java.util.*;
导入java.util.concurrent.TimeUnit;
导入java.util.logging.Level;
导入java.util.logging.Logger;
导入org.openqa.selenium.By;
导入org.openqa.selenium.Keys;
导入org.openqa.selenium.WebElement;
导入org.openqa.selenium.firefox.FirefoxDriver;
/**
*
*@作者穆罕默德·乌斯曼
*/
公共类JavaSelenium{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
System.out.println(“Hello Wrold”);
获取URL(“硒”);
}   
公共静态ArrayList GetUrls(字符串关键字)
{
火狐司机;
驱动程序=新的FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
驱动程序。获取(“https://www.google.com/?gws_rd=ssl"); 
WebElement;
qElement=driver.findElement(By.name(“q”));
qElement.sendKeys(关键字);
qElement.sendKeys(key.ENTER);
试一试{
线程。睡眠(3*1000);
}捕获(中断异常例外){
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE,null,ex);
}
//给出你想要抓取的网页的数量
int nPageToCrawl=5;
列出链接;
links=newarraylist();
对于(inti=0;i0)
{
System.out.println(“***点击下一页***”);
//点击下一页
driver.findElement(By.id(“pnnext”))。单击();
试一试{
线程。睡眠(3*1000);
}捕获(中断异常例外){
Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE,null,ex);
}
}
//它将获得页面链接的所有链接
List pageLinks=driver.findElements(By.cssSelector(“.r>a”);
用于(WebElement页面链接:页面链接){
links.add(pageLink.getAttribute(“href”);
System.out.println(pageLink.getText());
System.out.println(pageLink.getAttribute(“href”);
}
}
//关闭浏览器
driver.quit();
返回(ArrayList)链接;
}
}


如果有任何问题,请告诉我。

检查我发布的解决方案。这也是我第一个用Java编写的程序:)我会尝试让大家都知道:)
package javaselenium;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
/**
 *
 * @author Muhammad USman
 */
public class JavaSelenium {
/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    System.out.println("Hello Wrold");
    GetUrls("Selenium");
}   

public static  ArrayList GetUrls(String keyWord)
{
    FirefoxDriver driver;        
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    driver.get("https://www.google.com/?gws_rd=ssl"); 
    WebElement qElement;
    qElement = driver.findElement(By.name("q"));
    qElement.sendKeys(keyWord);
    qElement.sendKeys(Keys.ENTER);
    try {
        Thread.sleep(3*1000);
    } catch (InterruptedException ex) {
        Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
    }
    //give number how mange pages you want to crawl
    int nPageToCrawl=5;
    List links;
    links=new ArrayList();
    for (int i = 0; i < nPageToCrawl; i++) {            
        if(i>0)
        {
            System.out.println("***Click on Next Page***");
            //Click on Next Page
            driver.findElement(By.id("pnnext")).click();
            try {
                Thread.sleep(3*1000);
            } catch (InterruptedException ex) {
                Logger.getLogger(JavaSelenium.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        //it will get all links of pageLinks
        List<WebElement>  pageLinks = driver.findElements(By.cssSelector(".r>a"));
        for (WebElement pageLink : pageLinks) {
            links.add(pageLink.getAttribute("href"));
            System.out.println(pageLink.getText());
            System.out.println(pageLink.getAttribute("href"));
        }
    }
    //close browser
    driver.quit();
    return (ArrayList) links;
}