Praslea
Praslea

Reputation: 1

Selenium Java - unnable to click on Google Search

I saw a lot of threads with this issue but none is working for me as I tried almost all possible methods and still get error "Exception in thread "main" org.openqa.selenium.ElementClickInterceptedException: element click intercepted"

System.setProperty("webdriver.chrome.driver", "C://Driver//chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("https://www.google.com");

            driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]")).sendKeys("568567546754");
    driver.findElement(By.xpath("//form[@role='search']/div[2]/div[1]/div[3]//center/input[1]")).click();
    
    //driver.findElement(By.xpath("//div[@class='RNNXgb']/div/div[2]/input")).sendKeys("3462355452354");
    //Parent-child relationship xpath - Define xpath for parent
    //body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]
    
    //driver.findElement(By.xpath("//body/div[1]/div[3]/form[1]/div[2]/div[1]/div[1]/div[1]/div[2]/input[1]"));
    //driver.findElement(By.xpath("//div[@class='FPdoLc tfB0Bf']/center/input[1]")).click();
    //driver.findElement(By.xpath("//iframe[contains(@src, 'consent.google.com')]")).click();;
    //Thread.sleep(2000);
    //driver.findElement(By.xpath("//*[@id='introAgreeButton']/span/span")).click();
    //driver.findElement(By.linkText("Google Search")).click();
    //driver.findElement(By.id("lst-ib")).sendKeys();

    //driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
    //driver.findElement(By.name("btnK")).click();
    //driver.manage().window().maximize();

Upvotes: 0

Views: 260

Answers (3)

Apps Maven
Apps Maven

Reputation: 1420

You can use Action class here.

driver.get("https://www.google.com/");
driver.findElement(By.xpath("//input[@title='Search']")).sendKeys("568567546754");
Robot ab = new Robot();
ab.keyPress(KeyEvent.VK_ENTER);

Upvotes: 1

Priyanka
Priyanka

Reputation: 53

Please try this, it will work. As Enter will do the same thing as clicking on the Search button.

driver.findElement(By.name("q")).sendKeys("568567546754" + Keys.ENTER);

Here I'm not using the xpath for search text field as I've found name attribute. You can send the search text and Enter in one shot.

Upvotes: 1

Rahul Das
Rahul Das

Reputation: 74

You can use this reduced Xpath : //input[@name='q'] or driver.findElement(By.name("q")).sendKeys("568567546754");

or you can try this

JavascriptExecutor js = (JavascriptExecutor) driver;  
js.executeScript("\$x("//input[@name='q']")[0].value="Car"\");

check out this code snippet

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;

public class SearchAction{
   public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","./chromedriver.exe");
      WebDriver driver = new ChromeDriver();
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      driver.get("https://www.google.com/");
      // identify element
      WebElement p=driver.findElement(By.name("q"));
      //enter text with sendKeys() then apply submit()
      p.sendKeys("Selenium Java");
      // Explicit wait condition for search results
      WebDriverWait w = new WebDriverWait(driver, 5);
      w.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//ul")));
      p.submit();
      driver.close();
   }
}

Upvotes: 0

Related Questions