I am having trouble clicking the button in the popup dialog titled "Before you continue" on Google while using Selenium

I need help with clicking a button in dialog forms on Google. Here is the code I am using:

@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 is unable to locate the agree Webelement.

I agree

Answer №1

To successfully locate the button in an iframe, you need to include the following code snippet before attempting to find it: driver.switchTo().frame(0);

This should be placed prior to locating the button with the ID "introAgreeButton" using the code:

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

If the button is nested within an iframe, the driver must first switch to that frame in order to locate it.

Answer №2

Another option is to include the following code snippet:

driver.wait(until.ableToSwitchToFrame(0));
rather than using
Thread.sleep(4000);driver.switchTo().frame(0);

This method will handle the frame switch automatically

Answer №3

Here is a snippet of code that could be useful for the task at hand. Find and click on the agree button within the consent form.

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

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Utilize user input to fetch data from an external API

Let's say there is a field for 'part number' input that is not enclosed in a form tag. Whenever a user enters a value, the onblur event or a button positioned next to the input field should trigger a query to an external site via its API and ...

Inserting a line break in real-time within a JSX statement

Currently working on a React application that retrieves song lyrics from an API. The API provides me with a lyrics_body attribute as a string, which I use to showcase the lyrics on the webpage. However, when React renders it, the format is not ideal becau ...

How can a function in React Native be programmed to retrieve and utilize the most current variable values rather than relying on outdated ones

Is there a workaround for refreshing all variables in react-native after a function call? const [answer, setAnswer] = useState('hi'); const foo = () => { while (true) { console.log(answer); }} <Button onPress={() => setAnswer(&qu ...

Trouble in testing: ComponentDidMount is mysteriously bypassed by Jest/Enzyme when it's supposed to be triggered

In my code, there is a method called componentDidMount that triggers the fetchUser() function. I am currently working on testing the componentDidMount method. Here is the Component Code: static propTypes = { match: PropTypes.shape({ isExact: Pr ...

ng-view does not support ng-repeat rendering

I have a basic app using ng-view and ng-repeat. Initially, it worked fine without ng-view, but after switching to ng-view, the ng-repeat stopped functioning correctly. Oddly, when I clicked on the "menu" link, it displayed another set of $var instead of ch ...

Pass a customized field as an argument to the .find() method in mongoose

When making an HTTP call to my api to fetch documents from a mongodb, I include two parameters in the URL: campo and keyphrase. My goal is to retrieve all documents where the parameter campo is set to my specified keyphrase. For example, if I have some doc ...

Python's Selenium is unable to locate an element by ID on the Supreme website

Currently, I am developing a bot that is designed to purchase items from the supreme website using Python 3.5's Selenium library. The bot successfully adds an item to the cart, but encounters an error during the checkout process when attempting to sen ...

Passing Down Instance Methods Using Static References in JavaScript/TypeScript

✋ Exploring the concept of access modifiers from TypeScript, how can we make it relevant for JavaScript developers as well? Let's consider a scenario where a parent class defines necessary members and a shared method: // ParentClass.js export defaul ...

The way Firefox handles CSS rotation is distinct from the rotation method used in

I am attempting to create a 3D rectangle (parallelepiped) that users can manipulate using arrow keys. The functionality works smoothly in Chrome, but I have noticed discrepancies in the transitions when testing in Firefox. Please take a look at this fiddle ...

Troubleshooting a Custom Menu Control in HTML

Would you mind taking a look at the menu I have set up in this fiddle: http://jsfiddle.net/Gk_999/mtfhptwo/3 (function ($) { $.fn.menumaker = function (options) { var cssmenu = $(this), settings = $.extend({ title: "Menu", ...

Determining the orientation of an image in JavaScript

Currently, I am attempting to determine the orientation of images using JavaScript in order to apply a specific class to them. This process seems to be functioning correctly on Firefox, but is presenting challenges on other browsers. It appears to work bet ...

Generating a test report with selenium web driver in 5 easy steps

Are there any alternatives for generating test reports in Selenium WebDriver besides the TestNG framework? ...

I am able to select the checkbox in Internet Explorer and Firefox browsers, but for some reason I am unable to do so

Having trouble clicking on a Checkbox in Chrome, but no issues with IE and Firefox. It's puzzling me. Still searching for a solution. Any help would be appreciated. The input id is: <input id="ctl00_ctl55_g_0704d850_6481_416a_a414_9b4c51c8672f_c ...

What is the process for verifying the size of file uploads using Multer in Express?

Currently, I am utilizing Multer as the `multipart/form-data` middleware in Express. My main query revolves around how to verify the size of uploaded files, especially during the uploading process. I understand that you can define limits in the options ob ...

How can you utilize Angular Signals in combination with HostBinding to dynamically update styles?

Within a component called app-test, the following code is present: @Input({ transform: booleanAttribute }) reverse: boolean = false; @HostBinding('style.flex-direction') direction: string = this.reverse ? 'column-reverse' : &ap ...

Differences between Arrays of Numbers and Objects in Typegoose

Exploring Typegoose and encountering a puzzling issue. I defined a class with a field meant to store an array of numbers like this: class A { ... some other properties ... @prop({ required: true }) public nums!: number[]; } Here's a snippet of ...

Activate the ability to enter data into a specific cell

I am trying to change the color of an input cell to pink when a user types an incorrect key. Although I can successfully change the color, I am facing difficulty in resetting it after they click on the cell. I have attempted using .prop("disabled", false), ...

I am receiving a high volume of row data from the server. What is the best way to present this information on a redirected page?

There is a scenario where Page 1 receives a substantial amount of row data in JSON format from the server. The goal is to present this information on Page 2, which will be accessed by clicking on Page 1. To accomplish this task, JavaScript/jQuery and PHP ...

Problem with loading CSS and JavaScript files on Node.js server

Recently, I delved into learning Node.js and created a basic server setup like this: // workspace const p = document.querySelector('p'); p.textContent = 'aleluja'; html { font-size: 10px; } body { font-size: 2rem; } <!DOCTYPE ht ...

Tips for stopping the hidden div from automatically scrolling

To optimize performance, I have decided not to create many .HTML files. Instead, I am putting the page content into a div so that it will display as follows: <div id="page1"> first page content</div> <div id="page2"> f ...