当前位置: 首页 > 知识库问答 >
问题:

谷歌搜索SeleniumWebDriver,试图验证结果;如果结果为true,则返回为false

谷善
2023-03-14

我正在尝试编写一个SeleniumWebDriver测试,它可以导航到Google,搜索一些东西,然后验证Google是否搜索了输入的内容。然而,下面的代码给出了一个“false”输出,当我看到它是真的时。

我在这个场景中找到的所有代码似乎都是Google Instant之前的代码,我无法在FirefoxDriver中关闭Instant。它还应该等待网页,图像,视频等酒吧被点击之前,检查任何东西;增加隐式wait值似乎对测试的结果或最终结果没有任何影响。在verifyTrue和assertTrue之间切换不会更改结果。

如果答案显而易见,我很抱歉,但我对SeleniumWebDriver(和Java)相对较新。

package mypackage;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.util.concurrent.TimeUnit;

public class NavigateSearchVerify {

    public static void main(String[] args) {

        WebDriver driver = new FirefoxDriver();
        String baseUrl = "http://www.google.com";
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

        driver.get(baseUrl);

        WebElement query = driver.findElement(By.name("q"));
        query.sendKeys("Hello");
        query.sendKeys(Keys.ENTER);

        WebDriverWait wait = new WebDriverWait(driver, 15);
        wait.until(ExpectedConditions.elementToBeClickable(By.id("top_nav")));

        Boolean assertTrue = driver.findElements(By.className("gbqfif")).contains("Hello");
        if (assertTrue == true) {
            System.out.print("Yes");
        } else {
            System.out.print("No");
        }    
    }    
}

共有2个答案

燕嘉颖
2023-03-14

通过调试,你知道这是真的吗?

在Boolean assertTrue启动时替换这些行,然后尝试以下操作:

if(waitForTextToLoad(By.className("gbqfif"),"Hello")){
   System.out.println("Yes");
}else{
   System.out.println("Yes");
}

并将以下内容从您的主菜单中删除:

public boolean waitForTextToLoad(By by, String text){
        boolean wbeWaitFor = (new WebDriverWait(driver,10))
                .until(ExpectedConditions.textToBePresentInElementLocated(by, text));
        return wbeWaitFor;
    }
闾丘选
2023-03-14

检查您的问题和代码后,我了解到:-

  1. 你想打开谷歌

驱动程序。findElements(按.className(“gbqfif”))。包含(“你好”) 不正确。

有4个以上的元素具有类名(“gbqfif”)。因此,当您使用findElements()时,将返回一个WebElements列表。您正在检查此列表是否包含“Hello”字符串对象。但该列表包含WebElement对象。

请尝试以下代码:驱动程序。findElement(按名称(“q”))。getAttribute(“值”)。包含(“你好”)

如果这对你有帮助,请告诉我。

 类似资料:
  • 我正在用Hibernate Search 4.5.1和Spring 4.0.5版本构建一个应用程序。我正在尝试索引以下类: 我正在构建一个junit测试用例,看起来如下所示: 我还注意到在luke lucene上,一些索引词的长度最多为6个字符,例如,一首歌的艺术家是“后代”,而索引中存储的词是“the”和“offspr”。第一个可以,但第二个不应该是“后代”。为什么要截断名字?

  • 假设我有bean,具有以下属性: 字符串名 字符串lastName Statusenum Status 如何返回计算结果为true的? 如果上述方法不可取,那么推荐的最佳实践是什么?

  • 我一直在使用Chromium谷歌语音API,最近我改用谷歌云语音API。自从Google云语音API发布以来,其性能在识别准确率方面似乎有所下降。此外,我还看到越来越多的“空结果”返回音频流。 我同时将音频流传输到多个不同的服务,谷歌云语音API返回空结果,而其他一些服务返回转录文本。让我想知道Chromium语音API和Google Cloud语音API的工作方式是否有任何改变? 我验证了音频的

  • 当我使用MS Graph API并调用时,我会得到这个响应 {“@odata.context”:https://graph.microsoft.com/v1.0/$metadata#sites”,“value”:[]} 请求ID→cfb68a16-eb50-42ab-950c-fbfeec4Def5c 但是我可以使用有响应 {“@odata.context”:https://graph.micro

  • 我在Google Directions API中搜索了文档,也在网上搜索了一下,没有找到我的难题的答案。我的机构开发了一个API,可以使用transit查找出发地和目的地之间最新的可能出发点,有几个返回“无结果”错误。然而,当我手动搜索谷歌地图时,我能够返回结果。 我的Google Directions API程序发出以下查询: https://maps.googleapis.com/maps/a

  • 我目前正在参与使用Liferay(6.1 GA2)的项目。Liferay搜索结果似乎提供了指向Web内容片段的链接,而不是指向包含这些片段的页面的链接。 你们中有人经历过这个问题吗?你知道怎么解决吗? 非常感谢朋友们。 最好的,阿尔贝托