0

I have a problem , click button in dialog forms in google. This is my code :

@BeforeEach
public void startDriver() throws InterruptedException {
    // Start Chrome browser
    System.setProperty("webdriver.chrome.driver", "C:\\gecko\\chrome\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.get("https://www.google.pl/?gws_rd=ssl");
}

@Test
@DisplayName("Test strony google")
public void testGoogle() throws InterruptedException, IOException {
    System.out.println("Rozpoczecie testu strony google");
    ModelStatistic modelStatistic = new ModelStatistic();
    System.out.println(modelStatistic.getTimestamp());
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    Thread.sleep(4000);
    WebElement agree = driver.findElement(By.id("introAgreeButton"));
    agree.click();
    Thread.sleep(3000);
    WebElement name = driver.findElement(By.cssSelector("input[name='q']"));
    name.sendKeys("Kamil Surma");
}

Selenium Cant's see Webelement agree.

I agree
4
  • Hi, what kind of error are you getting? Commented Sep 4, 2020 at 20:53
  • Which is Before you continue button on Google Home Page? Commented Sep 4, 2020 at 21:27
  • no such element: Unable to locate element: {"method":"css selector","selector":"#introAgreeButton"} (Session info: chrome=85.0.4183.83) Commented Sep 7, 2020 at 20:16
  • Before you continue is a name of dialog form when I write google.pl/?gws_rd=ssl it show , but I want to click button "I Agree" WebElement agree = driver.findElement(By.id("introAgreeButton")); Commented Sep 7, 2020 at 20:18

3 Answers 3

1

Add this: driver.switchTo().frame(0);

before that: WebElement agree = driver.findElement(By.id("introAgreeButton"));

The button is in iframe and driver can't find it.

1
  • 5
    Here is the code for Python in case someone needs it: driver.switch_to.frame(0) driver.find_element_by_id("introAgreeButton").click() driver.switch_to.default_content() print("begone foul demon of google terms")
    – Curious
    Commented Nov 13, 2020 at 10:04
0

You can also add this: driver.wait(until.ableToSwitchToFrame(0)); instead Thread.sleep(4000);driver.switchTo().frame(0);

It will switch automatically

1
  • Thank you very much , bug is resolved. WebDriverWait wait = new WebDriverWait(driver , 5000); wait.until(driver -> driver.switchTo().frame(0)); WebElement agree = driver.findElement(By.id("introAgreeButton")); agree.click(); Commented Sep 9, 2020 at 13:48
0

This code might help. locate the consent form agree button.

WebElement consent = driver.findElement(By.xpath("//*[@id=\"L2AGLb\"]/div"));
consent.click();

Not the answer you're looking for? Browse other questions tagged or ask your own question.