Search code examples
javaseleniumfirefoxgeckodriverselenium-firefoxdriver

Why does Selenium identifies one button instead of two buttons on Google Home Page


Program Objective : - Program is to search for the number of buttons present on a Google Search page using tagName anchor

Problem : - This program is returning result 1 instead of 2 as there are two buttons available on google search page

Test Data :-

  1. JAVA :- java version "1.8.0_121" Java(TM) SE Run-time Environment (build 1.8.0_121-b13) Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)
    2 Selenium Executable JAR files :-selenium-server-standalone-3.3.1
  2. FF BROWSER :- 52.0.1 (64-bit)
  3. Eclipse :- Eclipse IDE for Java Developers Version: Neon.2 Release (4.6.2) Build id: 20161208-0600
  4. GeckoDriver : -geckodriver-v0.15.0-win64
  5. Code trials:

    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());
        }
    }
    

Console Output :-

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

Solution

  • The question is pretty tricky. Needless to say when we access the URL of Google we are redirected to our respective Google mirror sites. For example google.com, Google.co.in, Now with localization enabled Google webservice there is a possibility that the number of button may vary in those mirror sites.

    The fact with number of buttons which puzzles me immensely is because of the buttons which are kept hidden. We generally term them as hidden element. Some of those buttons gets activated only on typical circumstances while majority are simply not visible to the end-user but yes they still exists. Though the reason for keeping those buttons hidden would be a separate discussion thread but they are necessary to implement diverse business logic.

    Now what is more interesting is the fact that a button necessarily may not start with a button tag. So just like this case if you are looking to count the number of buttons through the tag "tagName" it is highly possible that you will be getting incorrect results. There is a possibility that some buttons will be defined within input tag or some other tag. So to find the number of buttons on a particular webpage someone must construct an xpath following the pattern of all the buttons present on the webpage.

    Now coming to the question. It is not that clear from the question what the OP actually wanted to assert. But definitely number of visible buttons will differ from the number of buttons defined within the development environment.