0

Facing " org.openqa.selenium.ElementNotVisibleException: element not visible" though I am able to locate element through xpath in DoM structure.Tried with explicit and implicate waits, still the exception continues.

package google;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Trail {
    public static void main(String[] args) throws InterruptedException {

 System.setProperty("webdriver.chrome.driver","F:\\Automation\\Software\\chromedriver.exe");
            WebDriver driver=new ChromeDriver();

        // Opening URL

        driver.get("https://www.google.co.in/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@type='text' and @id='lst-ib']")).sendKeys("pineapple");
        Thread.sleep(1000);
        //giving input and clicking on search

        WebElement ele = driver.findElement(By.xpath("//input[@name=\"btnK\"]"));
       JavascriptExecutor executor = (JavascriptExecutor)driver;
       executor.executeScript("arguments[0].click();", ele);
        driver.findElement(By.xpath("//input[@name=\"btnK\"]")).click();

        // Trying to click the first link available

        WebDriverWait wait = new WebDriverWait(driver, 10);
        WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("/*[@id=\"rso\"]/div[1]/div/div[1]/div/div/h3/child::*")));
        element.click();

        /* WebDriverWait wait = new WebDriverWait(driver, 30);
        WebElement element=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("/*[@id=\"rso\"]/div[1]/div/div[1]/div/div/h3/child::*")));
        element.click();*/

}
}
2

1 Answer 1

0

To click() on the first link available from the Google Search Results you have to induce WebDriverWait along with ExpectedConditions clause set to elementToBeClickable and you can use the following code block :

System.setProperty("webdriver.chrome.driver", "C:\\path\\to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.navigate().to("https://www.google.com/");
WebElement search_field = driver.findElement(By.name("q"));
search_field.sendKeys("pineapple");
search_field.submit();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='rso']//div[@class='g']//h3/a"))).click();
System.out.println("Page Title is : "+driver.getTitle());
driver.quit();

Console Output :

Page Title is : Pineapple: Health Benefits, Recipes, Health Risks
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.