java - Selenium 网络驱动程序 : Extracting strings in a search result using a loop

标签 java selenium-webdriver

我试图用循环单击所有搜索结果,并从每个结果中获取标题字符串。因此它会点击结果尝试提取字符串。

    String title = null; 
    List <WebElement> links = driver.findElements(By.className("thumbnail"));
    ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());

    for(int i=0; i<1; i++){
        links = driver.findElements(By.className("thumbnail")); // this step is must, because whenever you go to other page all store WebElements in a list will wash out
        links.get(i).click();
   //it opens the search result in a new tab and gains focus on that tab
        WebDriverWait wait = new WebDriverWait(driver, 10);
        By addItem = By.xpath("//*[@id=\"HEADING\"]");

        // get the "Add Item" element
        WebElement element1 = wait.until(ExpectedConditions.presenceOfElementLocated(addItem));
        wait.until(ExpectedConditions.stalenessOf(element1));

        if(!driver.findElements(By.xpath("//*[@id=\"HEADING\"]")).isEmpty()) {
            title = driver.findElement(By.xpath("//*[@id=\"HEADING\"]")).getText();
        }
        else {
             System.out.println("Title is missing");
        }
        System.out.println(title);

        driver.switchTo().window(tabs.get(0)); //Switching to first tab
    }

该代码正在提取第一页上的标题,而不是它单击的页面上的标题。我还尝试提取其他字符串,例如地址、电子邮件等,但我只是对此进行测试。我该如何解决?任何帮助将不胜感激,谢谢!

最佳答案

我改变了一些事情。

  1. 我将所有定位器(以及一些其他声明)移至循环的顶部和外部,这样它们就不会在循环内重新声明,以便 .findElement() 可以引用它们 来电。
  2. 由于您仅使用当前窗口句柄,因此将变量类型更改为 String 并仅获取当前窗口句柄(而不是句柄集合),以便您可以切换回主窗口循环末尾的制表符。
  3. 在点击后立即移动了陈旧性检查,因为这是您需要的地方,并将其更改为等待刚刚点击的缩略图。
  4. 将 XPath 定位器更改为使用 ID,因为这就是您所引用的全部内容。它更快、更短、更容易阅读。
  5. 第二个等待现在等待元素集合,然后使用该集合来测试是否为空并获取集合中第一个元素的文本。

    By addItemLocator = By.id("HEADING");
    By thumbnailsLocator = By.className("thumbnail");
    List<WebElement> links = driver.findElements(thumbnailsLocator);
    String originalTab = driver.getWindowHandle();
    Set<String> tabs = driver.getWindowHandles();
    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    for(int i = 0; i < links.size(); i++)
    {
        links = driver.findElements(thumbnailsLocator); // this step is must, because whenever you go to other page all store WebElements in a list will wash out
        links.get(i).click();
    
        // it opens the search result in a new tab and gains focus on that tab
    
        // switch to the new window
        for(String handle : driver.getWindowHandles()){
            if (!handle.equals(originalTab))
            {
                driver.switchTo().window(handle);
                break;
            }
        }
    
        // get the "Add Item" element
        List<WebElement> addItems = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(addItemLocator));
    
        if(!addItems.isEmpty())
        {
            System.out.println(addItems.get(0).getText());
        }
        else
        {
            System.out.println("Title is missing");
        }
    
        driver.close(); // close current tab
        driver.switchTo().window(originalTab); // switch to original tab
    }
    

关于java - Selenium 网络驱动程序 : Extracting strings in a search result using a loop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50277930/

相关文章:

java - 在java中获取谷歌搜索结果而不被标记

selenium-webdriver - 有没有更好的方法来定位以下 HTML 代码的元素?

javascript - 登录操作后无法找到元素

java - 模型驱动接口(interface)是否会在 struts2 中构成安全漏洞?

java - getValueIsAdjusting() 在 JList 中禁用,但现在我无法连续两次选择一个选项

java - 如何修复以下错误 : "Error: Main method not found in class"

java - 可从带有蜂窝的 url 中绘制

Python Selenium 在同一 Firefox 窗口中打开 URL

c# - Selenium/Chrome - 选择没有 ID 的复选框输入

java - 检查浏览器是否已关闭