简体   繁体   中英

Selenium - I can't click button in dialog “Before you continue” in google

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

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.

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

It will switch automatically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM