java - 为什么 Selenium 在 Google 主页上识别一个按钮而不是两个按钮

标签 java selenium firefox geckodriver selenium-firefoxdriver

程序目标: - 程序是使用 tagName anchor 搜索 Google 搜索页面上存在的按钮数量

问题:- 该程序返回结果 1 而不是 2,因为 Google 搜索页面上有两个按钮可用

测试数据:-

  1. JAVA:- java 版本“1.8.0_121”Java(TM) SE 运行时环境(版本 1.8.0_121-b13)Java HotSpot(TM) 64 位服务器 VM(版本 25.121-b13,混合模式)< br/> 2 Selenium 可执行 JAR 文件:-selenium-server-standalone-3.3.1
  2. FF 浏览器:- 52.0.1(64 位)
  3. Eclipse:- 适用于 Java 开发人员的 Eclipse IDE 版本:Neon.2 发行版 (4.6.2) 内部版本号:20161208-0600
  4. GeckoDriver:-geckodriver-v0.15.0-win64
  5. 代码试验:

    package com.packt.webdriver.chapter1;
    
    import java.util.List;
    import org.openqa.selenium.*;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class GoogleSearchPageByTagName
    {
        public static void main(String[] args)
        {
            WebDriver driver;
            System.setProperty("webdriver.gecko.driver","C:\\Users\\PragatiChaturvedi\\Desktop\\Selenium Web Driver\\geckodriver.exe");
            driver =new FirefoxDriver();
            driver.get("http://www.google.com");
            List<WebElement> buttons =driver.findElements(By.tagName("button"));
            System.out.println(buttons.size());
        }
    }
    

控制台输出:-

1490117595600   geckodriver INFO    Listening on 127.0.0.1:38505
1490117596915   mozprofile::profile INFO    Using profile path C:\Users\PRAGAT~1\AppData\Local\Temp\rust_mozprofile.UbkCPgo5hof6
1490117596926   geckodriver::marionette INFO    Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe with args []
1490117596999   geckodriver::marionette INFO    Connecting to Marionette on localhost:51031
Mar 21, 2017 1:33:55 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
1

最佳答案

这个问题很棘手。不用说,当我们访问 Google 的 URL 时,我们会被重定向到各自的 Google 镜像站点。例如 google.com、Google.co.in,现在启用了本地化的 Google Web 服务后,这些镜像站点中的按钮数量可能会有所不同。

按钮数量让我非常困惑的事实是因为按钮被隐藏了。我们通常将它们称为隐藏元素。其中一些按钮仅在典型情况下才会激活,而大多数按钮对最终用户来说根本不可见,但它们仍然存在。虽然隐藏这些按钮的原因是一个单独的讨论线程,但它们对于实现不同的业务逻辑是必要的。

现在更有趣的是,按钮不一定以按钮标签开头。因此,就像这种情况一样,如果您想通过标签“tagName”来计算按钮的数量,那么您很可能会得到不正确的结果。有些按钮可能会在输入标签或其他标签中定义。因此,要查找特定网页上的按钮数量,必须遵循网页上所有按钮的模式构建 xpath。

现在进入问题。从这个问题来看,并不清楚OP实际上想断言什么。但可见按钮的数量肯定会与开发环境中定义的按钮数量不同。

关于java - 为什么 Selenium 在 Google 主页上识别一个按钮而不是两个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42934633/

相关文章:

java - 这个 Spring JSON 端点在 Jboss/Tomcat 中有什么问题?

java - 使用 JNA 解密使用 MS DPAPI 加密的数据

javascript - 类型错误 : Cannot read property 'textToBePresentInElementValue' of undefined

selenium - Selenium IDE 中的 XPATH : Matching text in a <td> tag that contains <br> tags

css:Firefox 4 css 中的意外边距/填充

html - Firefox 中的 Simpletip 位置

java - 字节码操作的危险是什么(如果有的话)?

java - 线程 "main"java.lang.NumberFormatException : For input string: "t1"? 中的异常

c# - 如何并行运行 NUnit(Selenium Grid)测试?

javascript window.location 在 Firefox 中不起作用