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

What causes the return of 'undefined' during the execution of type coercion?

Can you explain why the type of a variable changes but the value remains undefined when trying to set it? let average; // Do I need to initialize with 'average = 0;' for it to work properly? for (const [oddName, odd] of Object.entries(game.odd ...

Issues arising from the event target

What is the reason behind this code functioning successfully only when the alert function is called? The color changes after closing the alert box, but if the line with the alert command is commented out, nothing happens. function setLinkColor(el) ...

Exploring FabricJs: Reassessing the coordinates of an object post-rotation

const canvas = new fabric.Canvas("c", { selection: false }); const refRect = new fabric.Rect({ left: 250, top: 150, width: 200, height: 100, fill: 'transparent', stroke: 'blue', originX: 'center', originY: & ...

What is the best way to retrieve nested reference data all at once from a Firestore document?

I have two distinct collections stored within my Firestore database. The first collection is users, and it can be visualized as shown here: https://i.stack.imgur.com/AKlyM.png The second collection is posts, represented by the following structure: http ...

Using `getJson` to parse the tree structure

I am currently working with a JSON file that contains order data. The structure of the JSON file is as follows: { "orders": [ {"name": "Peter", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="64140110011624050b0 ...

What mechanism does the useState() function utilize in React to fetch the appropriate state object and function for a functional component when employing the state hook?

Currently focusing on deepening my understanding of the useState hook in react. I have a question regarding how useState retrieves the state object and modifier function specific to each functional component when it is called. What I'm wondering is w ...

How to use Selenium WebDriver in Python to upload a file using a hidden input field

Html: <div id="js-cert-file" class="form-group"> <button id="js-ob-browse-n-upload" class="btn btn-ob browse-and-upload-onboarding-ssl-button" style=""> BROWSE & UPLOAD </button> <input id="js-cert-file" class="hidden btn btn-ob" ...

HTML table row content should be aligned to the left side

I am attempting to align the data in the 'Address' column without any margin. I want it to start from the left since it's overflowing. You can find the HTML, CSS, and JS code here Even though I tried using <td align="left">..</td& ...

Can a before hook ever run after a test in any situation, Mocha?

My before hook runs after the initial test and at the conclusion of the second test. Here is the code for my before hook: before(function () { insightFacade.addDataset("courses", content) .then(function (result: InsightResponse) { ...

Is it possible to modify the CSS styles for a specific portion of an input value?

Currently, I am in the process of developing a form using React.js where a specific input must match the label written above it. Here is an example: https://i.stack.imgur.com/S2wOV.png If there happens to be a typo, the text should turn red like this: h ...

Python PhantomJS fails to retrieve entire HTML content

From what I understand, there are various methods for web scraping: 1- using basic requests and bs4 If there are scripts on the webpage: 2- utilizing Selenium along with bs4 3- employing PhantomJS Currently, I am attempting to scrape I initially thou ...

Django RGBField cannot locate jQuery

Working on a project that utilizes an RGBField, the following script is injected into the template (nested deep within django's structure, as its exact location remains elusive): <script type="text/javascript"> (function($){ ...

Canvas - Occasionally, using lineTo() can result in crisp edges in the drawing

I've created a simple Canvas drawing app. However, I've noticed that occasionally the lineTo() command generates a line with fewer coordinates, resulting in many edges: I'm currently using the latest version of Firefox - could this issue be ...

Following the update from version 3.0.1 to 3.1, Android Studio refuses to launch

Recently, I updated my Android Studio from version 3.0.1 to 3.1 (32 bits). However, after the full update, I am encountering an issue where Android Studio is not starting up. I have included screenshots of the error for reference https://i.sstatic.net/c9S ...

Limit the rotation of jQuery and CSS3 elements to always turn in a clockwise direction

Utilizing the jQuery transit plugin, I am rotating a block element with 4 navigation controls. Each time a nav item is clicked, the block rotates to the specified custom data attribute of that nav item - either 0, 90, 180, or 270 degrees. While I have suc ...

Error in jQuery sortable function occurs when dragging multiple elements

When using the sortable() function on multiple lists, I encountered a persistent bug. To drag more than one item, I added the following code to the start function: e.item.siblings(".selected").appendTo(e.item); However, a new issue arose where the plac ...

Subscribing to topics dynamically with Spring Boot and ActiveMQ on-the-go

I have been researching how to implement a functionality where a listener class can subscribe/unsubscribe to JMS topics. After exploring various options, I have devised two potential solutions: Implement a listener class that maintains a list of topic na ...

typescript - transforming text into numerical values

newbalance = (Number(this.balance)) + (Number(this.pastAmount)); The result for my newbalance calculation is coming back as undefined, even though this.balance is 34 and this.pastAmount is 23. I've set this up in the controller and I'm trying t ...

Determine the fill color attribute value of a rectangle using Testcafe paired with typescript

Here is a code snippet I need help with: <colored-item label="Label A" symbol-size-left="9.5" symbol-size-right="12" symbol-right="" symbol-left="<svg viewport="0 0 24 24" xmlns="http://www. ...

Converting RowDataPacket to an array in Node.js and MySQL API, learn how to convert a RowDataPacket from the MySQL API into an array

Hello, I need assistance with converting my row data packet into an array of arrays or nested arrays. Please provide code snippet below: router.get('/getPosts/:user_id', (req, res, next) => { connection.query('SELECT * FROM files WHERE ...