Utilizing Javascript executor for selecting radio buttons

Having trouble selecting the 'Yes' and 'No' options on a page, I attempted to use JSExecutor since the normal 'Click()' function was not working. The issue is that while it is able to select the options, the value is not being sent as input, resulting in an error when attempting to proceed further.

I have experimented with various XPath methods without success!

WebElement st_1 = driver.findElement(By.id("app-select-no"));
 ((JavascriptExecutor)driver).executeScript("arguments[0].checked = true;", st_1);

Below is the HTML snippet:

div class="form-group input-form-group col-xs-6">
<input id="app-select-no" class="form-control ng-pristine ng-untouched ng-valid ng-valid-required" type="radio" ng-value="false" ng-model="openAccount.abc" required="" name="affRadioGroup" value="false" aria-checked="true" tabindex="0" aria-required="false" aria-invalid="false"/>
<label class="field-label-radio text-bold active" ng-class="{active : openAccount.abc==false}" for="app-select-no">
<span translate-once="NO_BUTTON">No</span>
</label>
</div>

Answer №1

It seems like you are attempting to toggle between "yes" and "no" on an element, but the issue may be that the element being clicked is actually an input field. In addition to clicking the element, you will also need to input a value into the field.

If the problem lies not in the click action with executeScript, you could try the following approach:

WebElement optionNo = driver.findElement(By.id("app-select-no"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", optionNo );

To input a value into the field, you can use this code snippet:

driver.findElement(By.id("st_1")).sendKeys("insert value here");

I hope this helps resolve your issue.

Answer №2

By rearranging the order of operations and ensuring the element is loaded before executing the javascript, I was able to resolve the issue. Below is the updated code that works for both scenarios - using javascript and without javascript (using webelement click):

driver.get("file:///home/sgupta/Desktop/a.html");
    WebElement radio = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#app-select-no")));
    JavascriptExecutor executor = (JavascriptExecutor)driver.getDriver();
    executor.executeScript("arguments[0].checked=true", radio);

Using the WebElement method :

driver.get("file:///home/sgupta/Desktop/a.html");
WebElement radio = new WebDriverWait(driver, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("input#app-select-no")));
radio.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

Identifying elements by class name in Selenium with Python when faced with multiple elements sharing the same class name

<div class="_2S1VP copyable-text selectable-text" data-tab="1" dir="ltr" spellcheck="true" contenteditable="true"></div> <div class="_2S1VP copyable-text selectable-text" data-tab="3" dir="ltr" contenteditable="true"></div> As a n ...

Achieve iframe resizing using only pure JavaScript, no reliance on jQuery required

I have a question about an iframe on my webpage. <iframe class="myframe" src="some_page.html" width="800px" height="600px" /> Is there a way to make this iframe resizable like the <textarea></textarea> tag? I prefer to achieve this wit ...

What is the reason behind the varying display of values?

When trying to set a value using the input tag, I encountered an issue. For example, if I type 1000.0009 into the input text, the valid value should be 1000.0001. However, the value displayed in the input tag is incorrect, while the value outside the tag i ...

Angular Typescript consistently returns false when comparing arrays

I've been working on comparing two nested arrays, but it keeps returning false. The goal is to compare the saleLineList in each saleBlock from this.saleBlockList with the saleLineList in each saleBlock from saleBlockList. Even after modifying the da ...

Is the variable empty outside of the subscribe block after it's been assigned?

Why is a variable assigned inside subscribe empty outside subscribe? I understand that subscribe is asynchronous, but I'm not sure how to use await to render this variable. Can someone please help me and provide an explanation? I am attempting to retr ...

Automatically generated JavaScript input value calculation

I have 3 text input fields set up as follows: <input type="text" id ="text1"/><br> <input type="text" id ="text2"/><br> <input type="text" readonly="readonly" disabled="disabled" id ="text3"/> The third input is disabled and ...

Stop Google Charts from magnifying the view

I'm currently working on creating a column chart using Google Charts. The purpose of this chart is to display the number of pageviews for a specific page within the last 30 days. Below is the JavaScript code I used to create the chart (you can access ...

Sprockets could not locate the file for jquery.atwho

I have been attempting to integrate the jquery-atwho-rails into my application, a Rails gem designed for at.js. I have followed all the steps provided, executed bundle install, included the necessary code in both application.js and application.css, stopped ...

Tips on Guaranteeing AJAX Requests are Successfully Called in Sequential Order and Receive Responses in the Same Sequence

What is the best way to guarantee that AJAX requests are executed in a specific order and receive responses in the same order? ...

Employing AJAX, execute a synchronous function asynchronously (Javascript)

Here's a code snippet for the sync function. I've been calling it inside an Ajax, but apparently it's deprecated because it's synchronous. Is there any way to make it run as if it were asynchronous? Or is there a way to convert it into ...

What is the best way to locate an element using Selenium?

I have been working on my code, making changes, fixing issues, and trying different modules. However, I am still unable to extract the text from my div element successfully. import selenium from selenium import webdriver driver = webdriver.Firefox() dr ...

Modifying the data attribute within the div does not result in a different image for the 360-degree spin view

My current project involves utilizing js-cloudimage-360-view.min.js to create a 360-degree view of images. I have successfully retrieved the images, but I am encountering difficulty in updating the images by clicking a button. index.html <!DOCTYPE html ...

The Jquery Text Function Exhibits Limited Functionality After Two Uses

Having some trouble with this script - it seems to only work correctly twice. The idea is to enter a city name in the text box, submit it, and then trigger a function. However, for some reason it only works two times. $("#b").click(function() { var p ...

Why is Jasmine throwing an error when I try to use getElementsByTagName(...)?

HTML: <ul id="listONE"> <li class="{{isSel}}" ng-repeat="person in people" ng-click="selPersonToChange(this)">{{person.name +" - "+ person.city}}</li> </ul> A snippet from my script.js using AngularJS (1.3.1): mymod.control ...

Tips on utilizing the useState hook for storing numerous key objects?

Currently, I am working with a candlestick chart for a cryptocurrency that displays data over different timeframes such as 1 minute and 30 minutes. Below is the code snippet that sets the initial state to show a 1-hour chart when a user first visits: const ...

Incorporating an external script within a Next.js application

I've been having trouble incorporating an external JavaScript source into my Next.js application and I keep encountering the following error: Failed to execute 'write' on 'Document': It isn't possible to write into a documen ...

What is the best way to retrieve information from a function that returns an AJAX GET JSON response?

There is a function in my code that uses ajax to return JSON data: function fetchTagData(fileName) { $.ajax({ type: "GET", dataType: "json", url: "/tags/find-tag/"+fileName.tag, success: function(data){ con ...

Navigating through a JavaScript object array within another object

I am looking to iterate through a JavaScript object array Here is my object response: { "kind": "calendar#events", "etag": "\"p3288namrojte20g\"", "summary": "pedicura", "updated": "2019-05-01T14:25:51.642Z", "timeZone": "America/Argentina ...

Starting with NodeJS: Troubleshooting module not found error

I'm new to NodeJS and I'm following the guide on the official NodeJS site to create a server. I have successfully installed NodeJS on my computer and created an app.js file with the code below: const http = require('http'); const host ...

Steps to follow in order to press the follow button on Instagram

Currently, I am attempting to automate the process of clicking the follow button on Instagram using Python Selenium. You can find the Instagram page I'm referring to at I've tested out the code below but unfortunately, it doesn't seem to be ...