0
votes

enter image description hereNot able to click the inline element using selenium webdriver.

Here is the URL https://www.google.com/.

Besides Images link (Right side top) there is a square icon. Need to click that icon and select Maps. Screenshot attached. I used xpath, cssselector, ID, Name but nothing is working. Could anyone help me on this.

Code:

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class webelements2 {
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.findElement(By.id("gbwa")).click();
        driver.findElement(By.className("gb_3")).click();               
    }       
}
5

5 Answers

0
votes

This code is somehow not working in Firefox because it opens the Products page when you click on element - By.id("gbwa"). But if you try the same in Chrome, it works fine. Only thing you would need to change is By.className("gb_3") with By.xpath("//ul[@class='gb_ka gb_da']/li[3]").

WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.id("gbwa")).click();
driver.findElement(By.xpath("//ul[@class='gb_ka gb_da']/li[3]")).click();

For Firefox, you can modify the code so that when the products page is opened, you can click on the Maps option from there.

0
votes

Some dynamic waits need to be added for controls. Additionally, I noticed that, if google is not set as your home page then you see a message 'Come here often? Make Google your homepage.'. This message needs to be handled, otherwise it will fail the access to the apps icon.

import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class webelements2 {
    public static void main(String[] args) throws InterruptedException 
    {
        System.setProperty("webdriver.gecko.driver","C:\\Users\\rpremala003\\Downloads\\geckodriver-v0.14.0-win64\\geckodriver.exe");
        WebDriver driver = new FirefoxDriver();
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 10);
        if (driver.findElements(By.xpath("//a[@title='No thanks']")).size() !=0) {
            wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@title='No thanks']"))));
            driver.findElement(By.xpath("//a[@title='No thanks']")).click();
        }

        driver.findElement(By.xpath("//div[@id='gb']//div[@id='gbwa']/div/a[@title='Google apps' and @role='button']")).click();

        wait.until(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']"))));
        driver.findElement(By.xpath("//a[@id='gb8']/span[text()='Maps']")).click();               
    }       
}

Try this code and let me know, if you have further queries.

0
votes

I've tested the code below and it works.

driver.get("https://www.google.com");
driver.findElement(By.cssSelector("a[title='Google apps']")).click();
new WebDriverWait(driver, 3).until(ExpectedConditions.elementToBeClickable(By.id("gb8"))).click();

You basically need to click the Apps icon, wait for the Maps icon to be clickable, and click it.

I'm assuming you don't work for Google so in that case, why test the UI? You can just directly navigate to https://maps.google.com and skip the UI clicks.

0
votes

How about this ... worked for me.

    driver.get("https://www.google.com/");

    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']"))));
    driver.findElement(By.xpath(".//*[@id='gbwa']//a[@title='Google apps']")).click();

    //click map icon
    driver.findElement(By.cssselector("a#gb8")).click();
0
votes

Use this, working for me.

Use Chrome driver instead of Firefox driver.

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class GoogleMaps {
    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//*[@id='gbwa']")).click();
        driver.findElement(By.xpath("//li/a[@id='gb8']")).click();
        Thread.sleep(6000);
        driver.quit();
    }
}

Output :

enter image description here