java - 从不可见页面获取信息

标签 java selenium-webdriver

要求:访问 Indeed.com 在哪些领域使用 QA 工程师,在哪些领域使用华盛顿州西雅图。 从除亚马逊或自动化之外的所有页面打印职位描述/标题

问题:在 Firepath 中,我使用的 xpath//div[contains(@id,'p')][contains(@class,'row')] 选择所有 职位在第一页。但是,当我执行下面的代码时,它只打印第一页的第一个作业描述 一遍又一遍,同时不断点击其他页面。

我得到的输出:

高级质量保证工程师 - 土木... Sound Transit - 12 条点评 - 华盛顿州西雅图 每年 $79,626 - $99,533 导师、辅导和指导 QA 工程师保持既定的工作标准。通过向 QA 提供指导来支持 QA 小组内的一致性... 保存工作 赞助

代码:

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;
import org.openqa.selenium.interactions.Actions;


public class QAJob {
    int MAX_PAGES;

    @Test
    public void jobSearch(){
        WebDriver driver= new FirefoxDriver();
        driver.get("https://www.indeed.com");
        driver.findElement(By.id("what")).sendKeys("QA Engineer");
        driver.findElement(By.id("where")).clear();
        driver.findElement(By.id("where")).sendKeys("Seattle,WA");
        driver.findElement(By.id("fj")).click();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        // Close the pop up window that appears
        driver.findElement(By.id("prime-popover-close-button")).click();

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

        //Find and print the number of pages found for search       
        List<WebElement> search_pages=driver.findElements(By.xpath("//div[contains(@class,'pagination')]//a"));
        System.out.println("Number of pages found for job search is " +search_pages.size());

        //Code to get and print job descriptions,title  
        List<WebElement> job_desc=driver.findElements(By.xpath("//div[contains(@id,'p')][contains(@class,'row')]"));

        for(WebElement e: job_desc){
            //using String so that I can use 'contains'
            String str_job_description=e.getText();

                while(search_pages.size()!=0){

                    //find Next link and click on it till the size is !=0 to get to last page
                    driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click(); 
                    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
                    //Do not want Amazon or Automation jobs
                    if(!((str_job_description.contains("Automation")) || (str_job_description.contains("Amazon"))) ){
                        System.out.println(str_job_description);
                    }

                }
            }
        }
    }

我可以使用一些有用的建议/想法。预先感谢您的宝贵时间。

最佳答案

对于 job_desc(外部 for 循环)中的每个元素,您都可以单击下一个按钮,直到到达最后一页(内部 while 循环)。 for 循环应位于 while 内。你可以尝试这样的事情(未经测试)

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_description=e.getText();
        if(!((str_job_description.contains("Automation")) || (str_job_description.contains("Amazon"))) ){
            System.out.println(str_job_description);
        }
    }
    driver.findElement(By.xpath("//span[contains(@class,'np')][contains(text(),'Next')]")).click(); 
}

顺便说一句,不需要每次迭代都定义隐式等待 driver.manage().timeouts().implicitlyWait,只需在创建 driver 时定义一次。如果你想在那里等待,请使用 Expected Conditions 显式等待

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By by));

关于java - 从不可见页面获取信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41337507/

相关文章:

java - 将表中的多个 UI 元素放入数组中

webdriver - Selenium Webdriver 和 PageFactory 初始化 List<WebElement> 元素

java - SendKeys 导致 "Leav page"警报

java - 在 java 中打印 ArrayList 时的不同输出

java - 在哈希集或树集中查找单词?

java正则表达式

java - 如何在java中处理嵌套的xml文件?

Java 在方法中使用图形

java - 尽管在网页上可用,但无法单击某个元素

selenium-webdriver - 无法选择下拉列表