Struggling to navigate web pages with Selenium using Java is proving to be a challenge

I am currently working on using Selenium's HtmlUnitDriver and WebElement classes in Java to automate clicking the "Download as CSV" button on Google Trends.

The issue that I am encountering is that the button remains hidden until another settings menu button is clicked, but I am unable to trigger this action using WebElement.

Below is the snippet of my code:

/**
 * @args String, the term to search on Google Trends
 */
public static void main(String[] args)
{
    //initialize HtmlUnitDriver
    HtmlUnitDriver hud = new HtmlUnitDriver();

    //navigate to the 90-day Google Trends page for the specified term in args
    hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");

    //point to the first button for activation
    WebElement element = hud.findElement(By.id("settings-menu-button"));

    //click on the element
    element.click();
}

The error message I receive is: org.openqa.selenium.ElementNotVisibleException: You may only interact with visible elements

However, the settings menu button is clearly visible. What could be causing this issue?

This is my first attempt at creating such a program and using this library, so any guidance would be greatly appreciated. I am still in the learning process. Thank you.

Answer №1

Could you give this a shot?

public static void main(String[] args)
{
    //initialize an instance of the HtmlUnitDriver
    HtmlUnitDriver hud = new HtmlUnitDriver();
    wait = new WebDriverWait(hud , 120);
    //go to the Google Trends page for the specified term in the last 90 days
    hud.get("https://www.google.com/trends/explore#q=" + args[0] + "&date=today%203-m&cmpt=q&tz=Etc%2FGMT%2B8");
    wait.until(ExpectedConditions.presenceOfElementLocated(By.id("settings-menu-button")).click();

}

Answer №2

Make the switch to a mainstream browser like Firefox or Chrome:

ChromeDriver hud = new ChromeDriver();

Here's why:

HtmlUnit (Rhino), the javascript engine used by HtmlUnitDriver, is not utilized by popular browsers. If you are testing javascript using HtmlUnit, your results could vary significantly from those of mainstream browsers.

Headless browsers with JavaScript support through an emulated DOM may encounter issues with websites that utilize advanced or obscure browser features, or have functionality with visual dependencies such as CSS positions.

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

Moving the starting directory of a NodeJS application on Azure

My NodeJS app on Azure was initially written in Javascript with the app.js file located in the root directory. This file was automatically detected during deployment via Git. Recently, I converted the app to Typescript and now have a build directory, with ...

Is there a way to navigate to the adjacent values in a json array?

I've been struggling with this issue for quite some time now. I have a list of items that can be moved to a div when clicked. My goal is to navigate through the items in the list (json) by clicking on Next and Previous buttons. As someone who is rela ...

What causes this Selenium test to always fail unless I manually add a pdb breakpoint?

Currently, I am working on a Selenium LiveServerTestCase within a Django project that is responsible for testing an AJAX-powered page. The main issue I am facing is the delay in loading another item after the initial page loads. In order to test for this s ...

What is the best way to manage the cumulative index within two nested ng-repeats?

Within the scope of a directive, I am working with two ng-repeats. This directive is responsible for generating a table, and each table cell's data comes from a separate data source. This data source requires the cumulative index of the two repeaters. ...

Tips for incorporating dynamic URLs in Next.js

In my current project using nextjs, I am dealing with fetching images via an API. Right now, I am receiving the "Full image path" (for example, "https://myurl.com/image/imagename.jpg") without any issue. However, I need to figure out how to fetch the image ...

Using AngularJS, I can bind the ng-model directive to asynchronously update and retrieve data from

I am currently working with Angular to develop a preferences page. Essentially, I have a field in a mysql table on my server which I want to connect my textbox to using ng-model through an asynchronous xhr request for setting and fetching data. I attempt ...

Is there a way to import a module generated by typescript using its name directly in JavaScript?

I am trying to bring a function from a typescript-generated module into the global namespace of JavaScript. The typescript module I have is called foo.ts: export const fooFn = (): string => { return "hello"; }; Below is the structure of my HTML file ...

Tips for activating a deactivated input field in ReactJS

I am trying to create a feature where my textfields are automatically disabled, but can be enabled for editing by clicking an 'Edit' button. Clicking the button again should disable the textfields and save any edits made. I have experimented with ...

JavaScript: Creating a Function that Returns an Object with an Undefined `this.variable`

While using javascript, I encountered an issue with instance variables. Declaring a variable as "this.variable" works fine until my function returns an object. However, if the function returns a String or Number, there are no issues. But when it returns ...

An exception is not compatible with a Throwable conversion

I am currently working on macOS using JDK8. In the catch block, I have noticed that I need to specify the complete name of the exception like in this case (ArithmeticException e) instead of (Exception e) for the code to compile successfully. If I use (Ex ...

"Embed" three.js within SmartMS

Is it possible to incorporate this simple three.js example into Smart Mobile Studio without extensive wrapping? I attempted to copy the window.onload content into an asm section but was unsuccessful. <!DOCTYPE html> <html> <head> <t ...

Exploring the potential of extracting link values from Mockmail using Selenium in Play! Framework 1.2.4 Java application

Hey there, I'm currently testing a scenario using MockMail where an email is sent with a link for the user to click on in order to become a verified user. Here's the output from MockMail: 14:52:04,664 INFO ~ From Mock Mailer New email rece ...

Is there a way to provide a dynamic value for the p:remoteCommand ajax call?

My issue involves a p:dataTable that contains p:commandLink elements. I need to initiate an ajax call with parameters when the mouseover event occurs. After some research, it became clear that commandLink cannot trigger an ajax call on mouseover directly - ...

Obtain the title of a Youtube video with Selenium in C#

Hey there, I'm currently working on using C# Selenium to extract the title of a YouTube video. Unfortunately, the methods I've tried so far haven't been successful. I've already tested getting the text from other elements and it works f ...

How to Implement Click Actions on Elements in AngularJS

Just starting out with angularjs and I have a scenario with jQuery. handleClick(); function handleClick() { var doubleClick = false; $('#text span.word').on('click', function() { var that = this; setTimeout(funct ...

What is the best way to dynamically change the color of my component depending on the prop passed to it?

I am facing an issue with the color of my component changing based on the value of the prop 'level'. Despite using states to set the backgroundColor, all components end up having the same color due to the state being altered for every comment. I ...

Start up a server using Angular along with Node.js and Express framework

I am encountering an issue with configuring Express as a server in my Angular application. The app loads without any issues when accessing the HOME route, but when trying to access another route, I receive an error message: Cannot GET / This is how I hav ...

Using JavaScript to dynamically set a background image without the need for manual hard-coding

My attempt was to showcase images as a background image upon mouseover event for the div section with id 'message' by manually coding JavaScript functions for each image like this: Here is the HTML code inside the body section <div id = "mes ...

Sending HTML information to a Quarkus Template

I am currently utilizing Quarkus Mailer and Quarkus Template to develop an endpoint exclusively for sending emails. At the moment, it only accepts the subject, body, and recipient email addresses. By employing Quarkus Template, I have a standardized HTML t ...

Using Python with Selenium to verify if two WebElements are the same

How can I determine if two Selenium WebElements are identical? To begin, I collect a list of input_fields along with the first_input element: self.input_fields = driver.find_elements(By.CLASS_NAME, class_name) self.first_input = driver.find_element(By.ID ...