Trouble with clicking buttons in Java and Selenium web scraping

Hey everyone,

Currently, I'm working on a Java Selenium script that automates clicking and filling forms for me. I've written a line of code that's causing me some trouble. The intention is to click on a button, but it's not happening as expected. When I run the script, the bot scrolls down to the button but doesn't actually click on it. Clicking on the button should ideally open a new page. I'm unsure of what went wrong; can someone assist me with this? Appreciate any help!

Snippet of My Code:

public static void main(String[] args) {


    WebDriver driver = new FirefoxDriver();
    
    driver.manage().window().fullscreen();
    
    driver.get("https://deutsche-giganetz.de/");
    driver.findElement(By.xpath("/html/body/section/div/div[1]/div[2]/button[1]")).click();
    driver.findElement(By.xpath("/html/body/header/div/div/nav/div/div[1]/div[2]/ul/li[1]/a")).click();
    driver.findElement(By.xpath("/html/body/main/div[8]/div/section/div/div[1]/div[2]/div[3]/div[1]/div/div[5]/a")).click();
    
}

}

Error Details: Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 108.0, moz:accessibilityChecks: false, moz:buildID: 20221208122842, moz:debuggerAddress: 127.0.0.1:38233, moz:geckodriverVersion: 0.32.0, moz:headless: false, moz:platformVersion: 10.0, moz:processID: 15732, moz:profile: C:\Users\OKANAY~1\AppData\L..., moz:shutdownTimeout: 60000, moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, moz:windowless: false, pageLoadStrategy: normal, platformName: WINDOWS, proxy: Proxy(), se:cdp: ws://127.0.0.1:38233/devtoo..., se:cdpVersion: 85.0, setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}

Element: [[FirefoxDriver: firefox on WINDOWS (dae2e314-a112-48de-ada9-bd6ffce54a94)]
-> xpath: `/html/body/main/div[8]/div/section/div/div[1]/div[2]/div[3]/div[1]/div/div[5]/a]
Session ID: dae2e314-a112-48de-ada9-bd6ffce54a94
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)

I attempted using "findelement css selector" instead of "findelement xpath". However, implementing it via a link wasn't feasible due to multiple buttons named "Order Now". Despite extensive research efforts, I couldn't find an alternative solution.

Answer №1

To ensure smooth page loading, you should always set timeouts and check for JavaScript completion before interacting with elements on the page. Specifically, make sure the element is clickable before calling the click method.

Duration pageLoadTimeout = Duration.ofMinutes(1);

WebDriver driver = new FirefoxDriver();

driver.manage().window().fullscreen();
driver.manage().timeouts().pageLoadTimeout(pageLoadTimeout);

driver.get("https://deutsche-giganetz.de/");
WebDriverWait wait = new WebDriverWait(driver, pageLoadTimeout);

// Wait for the page to fully load
wait.until(webDriver -> ((JavascriptExecutor) webDriver)
                .executeScript("return document.readyState")
                .equals("complete"));

// Use By.className or By.xpath to locate an element 
WebElement button = driver.findElement(By.className("uk-button btn-white"));
wait.until(ExpectedConditions.elementToBeClickable(button));
button.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

Implementing context menus on the Material-UI DataGrid is a straightforward process that can enhance the user experience

I am looking to enhance my context menus to be more like what is demonstrated here: Currently, I have only been able to achieve something similar to this example: https://codesandbox.io/s/xenodochial-snow-pz1fr?file=/src/DataGridTest.tsx The contextmenu ...

Guide on transforming the best.pt model of YOLOv8s into JavaScript

After successfully training a custom dataset on YOLOv8s model using Google Colab, I now have the best.pt file that I want to integrate into a web app via JavaScript. I've come across mentions of TensorFlow.js as a potential solution, but I'm stil ...

Selenium headless mode causing TimeoutException issue

When attempting to retrieve inner HTML from an element, I encounter a timeout exception in headless mode. Disabling headless mode allows the operation to work properly, but I require the element to be accessible in headless mode as well. from selenium impo ...

Determining the successful completion of an ajax request using jQuery editable plugin

I recently started using Jeditable to enable inline editing on my website. $(".video_content_right .project_description").editable(BASE_URL+"/update/description", { indicator: "<img src='" + BASE_URL + "/resources/assets/front/images/indicator ...

Transform spaces into %20 from an HTML input field by utilizing JavaScript or jQuery

Hi there, I'm encountering an issue with the input field on my website where users can enter search terms. I am currently extracting the user's input value and adding it to a URL string. jQuery("#searchButton").click(function(){ var simpl ...

Using Conditional Rendering and ReactCSSTransitionGroup for Dynamic Animations

I have developed a small application that displays different components based on a Redux state. I am trying to add a "fade" animation when one of the components renders, but unfortunately, it is not working as expected. Here is my current setup: content.j ...

Keeping Your Chromedriver Current on Windows

Currently, I am in the process of updating my chromedriver.exe file following the instructions provided here. Encountering a "Session not created" exception while trying to open Chrome using Python selenium webdriver The issue lies in the fact ...

What's the best way to add line numbers to source code on an HTML webpage after parsing?

I am currently working with AngularJS and MongoDB. In my MongoDB data, there are some text elements that contain a \n, causing each line to be displayed on a new line based on the occurrence of \n. However, I also want to add line numbers to each ...

Angular: promptly exit out of ngClick event handler

I have a selection menu on my webpage that is essentially an unordered list, with each item in the menu formatted like this: <li ng-click='doCalc(5)'>Five</li> The doCalc function that is triggered by clicking on these items may tak ...

A guide to extracting functions from a `v-for` loop in a table

Beginner here. I am attempting to create a dropdown menu for the div with an id matching my specific name. For instance, let's say my table column names are: A, B, C. I only want to have a dropdown menu for column A. The template of my table looks ...

jQuery Multi-select - Event Handler for Selecting All Items

I am currently using a jQuery multiselect that has the select all option checked by default. When there is a change in the selected options, I reload the page data accordingly. Everything is working well except for the fact that clicking the 'Select ...

Check input validations in Vue.js when a field loses focus

So I've created a table with multiple tr elements generated using a v-for loop The code snippet looks like this: <tr v-for="(item, index) in documentItems" :key="item.id" class="border-b border-l border-r border-black text ...

Do developers only need type definitions for npm packages on their local machines?

It is my understanding that type definition modules for npm packages offer developers intellisense, eliminating the need to guess parameter types when using library methods. These modules have proven to be extremely valuable in my typescript project, esp ...

Error: Unable to locate npm package

I am currently working on an Angular application that was created using Grunt and relies on Bower and NPM. Recently, I attempted to install an npm module locally. The installation resulted in the files being stored in the main application directory under ...

Anticipated a JavaScript module script, but the server returned a MIME type of text/html as well as text/css. No frameworks used, just pure JavaScript

I have been attempting to implement the color-calendar plugin by following their tutorial closely. I have replicated the code from both the DEMO and documentation, shown below: // js/calendar.js import Calendar from '../node_modules/color-calendar&ap ...

Obtain the HTML document using C# Selenium once it has been altered

My current HTML source looks like this: <b class="number"> Click to see </b> After clicking on the above element, the corresponding JS code is executed: function fun(){ number.html("12345"); } Although utilizing driver. ...

Building custom directives on AngularJS pages without a specified ng-app module

On some of my basic pages, I don't need to specify a particular application module in the ng-app attribute. However, these pages do utilize some custom directives that I have created. To keep things organized, I have placed all of my directives withi ...

Guide on choosing a checkbox within a multiple select box using Selenium WebDriver in Java

I have the following code snippet for a multiple select box on a webpage, and I am looking to automate the selection of an option group using Selenium WebDriver. <div class="ms-drop bottom" style="display: block;"> <div class="ms-sear ...

Setting a fixed data value within a div for subsequent retrieval through a function

I found a helpful example that demonstrates how to convert numbers into words. You can check it out here. The function for converting numbers into words is implemented in the following HTML code: <input type="text" name="number" placeholder="Number OR ...

Stellar.js is malfunctioning

I've been attempting to implement a parallax effect using Stellar.js with two image tag elements, but I'm encountering issues. Despite trying various configurations, including following the Stellar.js creator's tutorial scripts closely, noth ...