java - 使用替代方法 try catch 页面末尾链接的异常

标签 java selenium

要求:转到最近 3 天的求职链接。 1) 打印职位描述 2)点击下一页链接进入下一页,直到到达最后一页

问题:当我到达最后一页时,我使用 try catch 找不到下一个链接的此类元素。使用此解决方案它会停止脚本,通过查看 JUNIT 栏,您将不知道测试是否通过或失败。它是一个灰色栏,因为我使用了 exit。我怎样才能使这段代码更好,这样我就不必使用 try catch 并看到绿色条来通过测试?

代码:

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

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class QAJob {

    @Test
    public void jobSearch(){
        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.indeed.com/jobs?as_and=qa+engineer&as_phr=&as_any=&as_not"
                + "=&as_ttl=&as_cmp=&jt=all&st=&salary=&radius=10&l=Bellevue%2C+WA&fromage=7&limit"
                + "=10&sort=date&psf=advsrch");
        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //code to scroll down to find the rest pages link
        JavascriptExecutor jse = (JavascriptExecutor) driver;
        jse.executeScript("window.scrollBy(0,1000)", "");

        // Find and print the number of pages for the search
        List<WebElement> search_pages=driver.findElements(By.xpath("//div [contains(@class,'pagination')]//a"));

        System.out.println("Number of pages found for this search " + search_pages.size());

        while(search_pages.size()!=0){
            List<WebElement> job_desc=driver.findElements(By.xpath("//div [contains(@id,'p')][contains(@class,'row')]"));

            for(WebElement e:job_desc){
                String str_job_desc=e.getText();
                System.out.println(str_job_desc);
            }
            try
            {
                // closes the pop up that appears 
                driver.findElement(By.id("popover-x-button")).click();
            }
            catch (Exception e)
            {


            }

            try
            {
                //click on next link to go to next page
                driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click();

                //scroll down
                JavascriptExecutor jse1 = (JavascriptExecutor) driver;
                jse1.executeScript("window.scrollBy(0,1000)", "");
            }
            //when I get the exception(because no next link is available) exit.
            catch (org.openqa.selenium.NoSuchElementException e)
            {
                System.exit(0);
            }
        }

    }
}

预先感谢您的时间和建议。

最佳答案

您实际上可以重复使用代码中使用的技巧来避免 try-catch block

List<WebElement> popXButton=driver.findElements(By.id("popover-x-button"));
     if (popXButton.size()>0){
         driver.findElement(By.id("popover-x-button")).click();
     }

同样的情况也扩展到下一个 block

List<WebElement> nextVal=driver.findElements(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]"));
if(nextVal.size()>0){
     driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click();
}
else{
  break;//exits while loop!
}

关于java - 使用替代方法 try catch 页面末尾链接的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41420948/

相关文章:

javascript - 使用 Java Selenium 的 Angular 4 等待条件

java - Spring + Hibernate Web 应用程序 - 何时使用缓存?

java - 在JSP显示表中显示HashMap键和值

java - 如何在单独的 J Frame Form 标签上显示 Java Timer?

java - 如何使用 Jenkins/Maven 运行我的 selenium 测试?

testing - 在 capybara 上使用自定义 Selenium 服务器

java - 如何使用java代码获取excel文件路径

java - 在 Java Servlet/输入字段结构中访问 HTML 输入字段数组

java - 在典型的 opengl 渲染引擎中,我应该如何加载和显示 Wavefront OBJ 文件

java - Selenium 处理多个窗口面临的问题