Having trouble running a Selenium WebDriver command on a button with JavaScript function enabled

I found this interesting resource on how to copy values from one field to another by clicking a button. I attempted to automate the process using Selenium WebDriver. Here is my script:

driver.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy");
driver.findElement(By.xpath(".//button[@onclick='myFunction()']")).click();

However, I encountered an error:

org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":".//button[@onclick='myFunction()']"}
Command duration or timeout: 20.11 seconds

After that, I tried an alternative approach:

JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById(\"field2\").value = document.getElementById(\"field1\").value;");

This time, I faced another error:

org.openqa.selenium.WebDriverException: document.getElementById(...) is null

Answer №1

To interact with the button inside an iframe, you must first switch to the frame to locate the element. Refer to the code provided below:

driver.get("http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_onclick_copy");  
driver.switchTo().frame(driver.findElement(By.id("iframeResult"))); 
driver.findElement(By.xpath(".//button[@onclick='myFunction()']")).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

Error: Unexpected character 'o' encountered in AngularJS syntax

Whenever I try to call this controller, it gives me an error. hiren.controller('hirenz' , function($scope , $http , $location , $routeParams){ $http.post((rootURL + "music") , {'alpha' : $routeParams.alpha , 'name' : $rou ...

Phonegap application functioning smoothly on computer, encountering issues on mobile device

Hey there! I recently developed a phonegap app that retrieves JSON data from a YQL link and presents it to the user. It works perfectly on Google Chrome desktop, but my client mentioned that it doesn't work on his Android 2.3 device. Could you help me ...

What are some solutions for repairing unresponsive buttons on a webpage?

My task is to troubleshoot this webpage as the buttons are not functioning correctly. Here’s a snippet of the source code: <!DOCTYPE html> <html lang="en"> <head> ... </head> <body> <div id="container" ...

What steps should be followed to set up Selenium WebDriver to accept command line options using Node.js?

I'm currently working with Selenium WebDriver through Node.js and I have a set of resources that I'm not sure how to interpret for my specific objective (here and here). At the moment, all my tests are running successfully as intended but now I w ...

Capturing data from HTML using Selenium for Web Scraping

Currently attempting to extract specific values from a website. Specifically, I am interested in retrieving the number of articles displayed on the site. I aim to capture this value (1) and store it in a variable for future use. However, I have encountered ...

Tips on revealing class titles during serialization using Gson

My situation is quite intricate, but here's the gist: I am delving into the inner workings of a compiler - creating JSON serializations of ASTs from various programs to better grasp what each node represents. However, there's a slight hiccup: th ...

Error: material not being loaded by objloader

I recently started working with Three.js and encountered an issue while loading an obj object into my scene. The object loads successfully, but the materials are not provided by the MTLLoader. I'm unsure if my object is broken or if there is an issue ...

Using the parent id in knockoutJs to create a nested menu

I'm currently attempting to create a nested menu using the JSON data provided by the client. Here is the data: var serverData = [ { Id: "menuColorSearch", Text: "Color search" }, { Id: "menuAncillaryProductM ...

Java Serialization and Deserialization Tool for MongoDB and Redis

Previously, I utilized the Jackson annotations @JsonSerialize and @JsonDeserialize to serialize Java objects to JSON and vice versa: public class Dummy { @JsonSerialize(using = CustomNumberSerializer.class) @JsonDeserialize(using = CustomNumberDes ...

Determining the file extension type of an uploaded file using JavaScript

In my new file upload system, users have the option to upload both images and videos. After uploading a file, I provide a preview of the uploaded content. Desired Outcome: My goal is to display only ONE preview based on the type of file (image or video). ...

Returning to the parent frame in Selenium after it has been removed from the DOM

I have a webpage with the following structure: <body> <iframe id="outer_frame"> <iframe id="search_button_frame"> <button id="search_button"></button> </iframe> </iframe> </body> When I cl ...

My Node JS program becomes unresponsive when reaching the resolve() method

I encountered a problem with my Node.js application when trying to list AWS Cognito users. The issue arises only when the number of Cognito users exceeds 60. API Reference Below is the snippet of my code. function checkUserPermissions(cognitoidentityse ...

Be warned: Babel has detected a duplicate plugin or preset error

Currently, I am enrolled in a React course on Frontend Masters. As part of the course, we were tasked with modifying the Babel config to allow state instantiations like: state = {index: 0} in class components. However, when I executed the command: npm i ...

What is the best way to transform a JavaScript object into a JavaScript literal?

Currently, in my nodejs project, I have an object defined as follows: const objA = { key : 'value' }; My goal is to create a new file named obja.js which should contain the same literals from the object, rather than as a JSON literal. How can I ...

Unexpected token . encountered in Javascript: Uncaught Syntax Error. This error message is triggered when

As someone who is completely new to programming, I was given the task of creating a voting website for a class assignment. In order to store data locally, I managed to create variables and implement them using local storage: var eventName = document.getEl ...

Resource for building user interface components in Javascript

I need assistance with implementing a feature that involves scrolling through different text blocks and corresponding images. Users should be able to select a specific text block and have the associated image on the right scroll into view, similar to a car ...

Square div with side arrow using CSS

I have a div containing an image that should only appear when an element is hovered over. I would like to add a side arrow pointing towards the direction of the hovered element. Below is the code for generating the square div: <div id="image-thumb ...

Guide on generating a dynamic loop in JavaScript using the group name as a variable

I have been attempting to organize a group, dynamically ungroup it, and then create a loop with each element of the group. Despite reviewing multiple examples, I have not been able to solve the following issue: The grouping is done based on the "tipo" pro ...

Ensure that functions are not executed within <Collapse> until it has been expanded while working with ReactJS and Material-UI

In my ReactJS application, I am using Material-UI to create an expandable table. When a row is clicked, more detailed information is displayed beneath that row. To see a simple example of this functionality, you can check out the following sandbox link: h ...

Spring Boot application is unable to read chromedriver.exe file from the resource folder

My Spring Boot project utilizes Selenium for automation testing of various applications, with the output being an archived JAR file. The code snippet below initializes the Chrome browser: static { try { Resource resource = null; String ...