简体   繁体   English

点击所有结果链接

[英]Click on all the result links

Scenario: Go to Yahoo.com search for hotmail and click on all the search pages one at a time. 场景:转到Yahoo.com搜索hotmail,然后一次单击所有搜索页面。

My code: It just prints the link of the result pages. 我的代码:它只是打印结果页面的链接。 Is there a way to click pages 2,3,4 while there is no page left? 有没有办法单击页面2,3,4而没有页面呢?

public class QAJob {

    @Test
    public void jobSearch(){
        WebDriver driver= new FirefoxDriver();
        driver.get("https://yahoo.com");
        driver.findElement(By.id("uh-search-box")).sendKeys("hotmail");
        driver.findElement(By.id("uh-search-button")).click();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        //scroll down
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,1000)", "");

        List<WebElement> result_pages=driver.findElements(By.xpath("//div[contains(@class,'pagination')]//a[contains(@title,'')]"));

        for (WebElement e: result_pages){

            Actions action= new Actions(driver);

            WebElement search_results=driver.findElement(By.xpath("//div[contains(@class,'pagination')]//a[contains(@title,'')]"));
            System.out.println(e.getAttribute("outerHTML"));    
        }
    }
}

In Selenium you can use the class name to find the element, in your case, the next link has the class next , so you need something like: 在Selenium中,您可以使用类名来查找元素,在您的情况下, next链接具有next类,因此您需要类似以下内容:

driver.findElement(By.className("next").click();

To get to the next page. 进入下一页。

Fox example this script print all the links (up to page MAX_PAGES ): Fox示例,此脚本打印所有链接(至第MAX_PAGES页):

    int MAX_PAGES = 3;
    driver.get("https://yahoo.com");
    driver.findElement(By.id("uh-search-box")).sendKeys("hotmail");
    driver.findElement(By.id("uh-search-button")).click();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    for (int i = 0; i < MAX_PAGES; i++) {
        for (WebElement link : driver.findElements(By.cssSelector("h3.title > a"))) {
            System.out.println("Title: " + link.getText() + " link" + link.getAttribute("href"));
        }
        driver.findElement(By.className("next")).click();
    }

You can change the MAX_PAGES variable to go further, or if you prefer to get all pages, you can catch the NotSuchElementException that selenium will throw when we reach the last page. 您可以将MAX_PAGES变量更改为更远,或者,如果您希望获取所有页面,则可以捕获到硒到达最后一页时抛出的NotSuchElementException

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用Java和Selenium Webdriver单击网页上的所有链接 - Not able to click all links on a web page using Java and Selenium Webdriver 如何单击所有产品链接,搜索元素并返回。 - How to Click at all product links, search an element and navigate back. 如何使用Selenium WebDriver逐个获取所有链接并单击这些链接 - How to fetch all links and click those links one by one using Selenium WebDriver 如何使用Selenium WebDriver来获取所有链接并单击一个链接 - How to fetch all links and click those links one by one with Selenium WebDriver 使用JSoup从Google搜索结果的所有页面检索所有链接 - Retrieving all links from all pages of Google search result using JSoup 链接并在ListView中单击? - Links and on click in ListView? 如何在selenium webdriver中从网页中提取所有链接后单击特定链接 - How to click on specific link after extracting all the links from a web page in selenium webdriver 获取网页中的所有非隐藏链接,然后使用Selenium Webdriver单击 - Get all the non-hidden links in a webpage and click using Selenium Webdriver 无法单击Selenium Webdriver中的所有链接。仅第一个链接被单击 - Not able to click on all the links in Selenium Webdriver.Only the first link gets clicked java,Webdriver,我想获取网页中的所有链接,然后将它们与我拥有的字符串名称进行比较,如果找到它,则单击它 - java,Webdriver, I want to fetch all links in a webpage and then compare them to a string name that I have and if it is found then click it
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM