Enable JavaScript in Webdriver Selenium to enhance the functionality of your web

I am facing an issue with my Java program and Selenium WebDriver. The script I have does not detect the button "Open device access" because its style is set to "display: none".

Usually, clicking on "Device Access" triggers JavaScript to display the "Open Access device" button. However, it seems that my Firefox WebDriver is not supporting JavaScript. How can I resolve this?


driver.get(baseUrl);
driver.findElement(By.id("username")).clear();
driver.findElement(By.id("username")).sendKeys("admin");
driver.findElement(By.id("password")).clear();
driver.findElement(By.id("password")).sendKeys("XXX");
driver.findElement(By.name("btnlogin")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//a[@href='/mybox/devices/overview.php']")).click();
Thread.sleep(5000);
driver.findElement(By.xpath("//a[@href='/mybox/devices/satellite.php']")).click();
Thread.sleep(5000);
WebElement element = driver.findElement(By.xpath("//input[@value='Open device access']"));
System.out.println("Element display (Before accordion): "+element.isDisplayed()+"");
driver.findElement(By.id("device_hmi_content_22")).click();
WebElement element2 = driver.findElement(By.xpath("//input[@value='Open device access']"));
System.out.println("Element display (open): "+element2.isDisplayed()+"");

if (isElementPresent(By.xpath("//input[@value='Close device access']")) ) {
    driver.findElement(By.xpath("//input[@value='Close device access']")).click();
    driver.findElement(By.xpath("//input[@value='Open device access']")).click();
    Thread.sleep(5000);
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div/p/span")));
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div[2]/input")));
    assertTrue(isElementPresent(By.xpath("/html/body/div[2]/div[3]/div[3]/div[2]/div/div[2]/div[2]/div/div[6]/div/div/div[2]/input[2]")));
    Thread.sleep(3000);
    driver.findElement(By.xpath("//input[@value='Close device access']")).click();
    Thread.sleep(5000);
} else {
    // Code to handle if element is not present
}

Although I prefer not to use JavaScript directly in my code, how can I enable JavaScript on my Firefox WebDriver?

The button "Open device access" is currently invisible:

<div id="device_hmi_content_22">
 <div id="accordion_device_hmi_22" class="accordion">
 <h2 class="accHeadline accHeadlineClosed">Device Access</h2>
 <div class="accContent accContentClosed ">
 <div class="submit">
  <input type="button" onclick="onOpenSessionClick()" value="Open device access">
 </div>
  </div>
 </div>

Since the "Open device access" button is not visible, how can I interact with it and make it visible?

Thank you for any assistance provided.

Answer №1

When using Selenium WebDrivers, such as the Firefox driver, JavaScript is designed to work seamlessly without any extra setup required. The problem does not lie in JavaScript not functioning properly. To confirm this, simply open the console during a prolonged pause and execute alert();. Alternatively, dispel any doubts by incorporating the following snippet into your test:

driver.get(baseUrl);
driver.execute('alert();');

If you witness the familiar browser alert, it indicates that JavaScript is indeed operational, implying that the issue may originate from elsewhere.

In my personal experience, I have encountered instances where a click on an element is intercepted by another overlapping element, particularly when dealing with fixed-position elements. If you suspect this might be causing the problem, consider adjusting the elementScrollBehavior to 1.

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

Unraveling bytes doesn't appear to unlock their code

When attempting to retrieve the html source of an "academic" website, I encountered decoding issues. My approach involved using the following requests commands: resp = requests.get(url) print(resp.content) edit: I also tried resp.text The output was as ...

Unable to handle JavaScript alert in Selenium WebDriver

During my testing, various alerts keep popping up. For instance, if I forget to fill out a mandatory field in the form, an alert is triggered. Up until now, I've been able to handle these alerts and perform necessary actions (such as verifying text an ...

Sending data from PHP to JavaScript using AJAX

I'm encountering an issue trying to pass data from a PHP file to a JavaScript file using echo. My PHP file contains the following code snippet: <?php ...establishing connection and retrieving list from database... for($i=0;$i<sizeOf($li ...

A helpful guide on using the Jackson library to import a JSON file into Java

Can someone help me understand how to read a simple JSON file using the Java Jackson library? I am new to working with JSON data and may have made mistakes in creating the classes or JSON objects, or in converting JSON objects to Java. Any guidance would b ...

Are there any tools available for measuring JavaScript code coverage that can be seamlessly integrated with Selenium Server or RC?

Exploring JavaScript code coverage tools compatible with Selenium Server/RC Hello, I am looking to incorporate JS code coverage into my Selenium Test Suite. Are there any code coverage tools that can seamlessly integrate with my current selenium rc fram ...

Incorrect color change on button triggered by onMouse actions and state fluctuations

While creating a user profile on my app, I encountered an issue with button coloring. When I try to add color functionality to the button, it turns red instead of green and remains red even when the mouse is not hovering over it. My goal is to have the but ...

Show a different image with a matching title each time the page loads

I need help creating a JavaScript script that will display a random image from an array along with an associated title each time the page loads. Is there a way to do this without using an onload function placed in the body tag? Currently, my code relies o ...

Strategies for locating web elements in a Selenium test script when an ID is not available

I am trying to locate and click on a button in the browser, but it doesn't have an ID or CSS class name. The button code is as follows: <button aria-describedby="notes" aria-label="Notes" autofocus="" class=" ...

Web scraping done purely from the command line, no need to open a browser window

I am looking for a way to scrape YouTube videos using Python without using pytube or any other modules. I want to achieve this using requests and selenium, but I do not want the browser window to open like Selenium normally does. While I can use requests ...

What is the process for generating a dynamic array in typescript?

Looking to create a TypeScript dynamic array with the desired format: const display = [ { id: 1, displayName: "Abc1" }, { id: 2, displayName: "Abc2" }, { id: 3, displayName: "Abc3" } ] Attempted the following code ...

The issue of the marker vanishing upon refreshing the page on AngularJS

Currently, I am encountering a rather peculiar issue. Upon the initial page load, the code snippet below correctly displays a marker at the specified coordinates and ensures the map is properly centered: <div class="paddingtop"> <map data-ng- ...

React error: File undefined (cannot be exported)

Encountering an issue while attempting to follow a tutorial: src/components/MyApp.js Line 17:18: 'myApp' is not defined no-undef Search for the keywords to learn more about each error. This is what my myApp.js file contains: import React fr ...

How to Redirect a Webpage to the Same Tab in ASP.NET

I am currently using an asp.net hyperlink control to direct users to a web URL when the hyperlink is clicked. My goal is for the user to open a new tab, rather than a new window, when they click the hyperlink. If the user clicks the link again, I want th ...

Getting the JavaScript file name within another JavaScript file - a simple guide

Imagine having an HTML file that references two external JavaScript files. One of these JavaScript files contains errors (likely compilation errors), while the other file includes an onerror method without any issues. When you open the HTML file in a brows ...

Sorting two different divisions is an example

I need advice on how to toggle between two divs, A and B, without having to reload the page. Ideally, I would like to have three buttons - one that shows only div A when clicked, another that displays only div B, and a third button that shows both A and ...

Push function is not available in Redux

Currently, I am facing an issue while trying to use the state of a component in other components. Since this state needs to be accessed by multiple components, I have decided to switch to Redux. However, I encountered an error 'newUsers.push is not a ...

Access Azure-Active Directory through cypress tests

Currently, I'm faced with the task of creating automated tests for an application that requires login to Azure Active Directory. These tests are being written using Cypress and TypeScript. In search of a solution, I am seeking advice on how to execute ...

Adding a class to an element within a React template using JS is not possible

I am currently using a Bootstrap template for my React app. Within the template, there is a JavaScript code that should make the header sticky when scrolling. It functions as expected in pure HTML, but once I move the header to a React template (necessary ...

issue with implementing the chart.js npm package

Just recently, I added chart.js to my project using npm. My goal is to utilize the package for creating graphs. npm install chart.js --save After the installation, I attempted to import the module with: import chart from 'Chartjs'; However, t ...

Restore the initial content of the div element

Currently, I am utilizing the .hide() and .show() functions to toggle the visibility of my page contents. Additionally, I am using the .HTML() function to change the elements inside a specific div. $('#wrap').html(' <span id="t-image"> ...