What is the best way to automate a website that has identical buttons with the same class, but different HTML onlick functions?

One of the buttons reads

<button class="btn bid-hotel-btn" type="button" onclick="buyNow(0,false)">Beli Sekarang</button>

while the other button is

<button class="btn bid-hotel-btn" type="button" onclick="buyNow(1,false)">Beli Sekarang</button>

The only difference lies in the onclick event attribute within the HTML. I am looking to automate the click of one of these buttons.

Answer №1

Consider utilizing XPath

//button[@onclick="buyNow(0,false)"]

for the initial button. Also, use

//button[@onclick="buyNow(1,false)"]

for the second one

Answer №2

I don't have much knowledge about javascript, but you can still refer to the example below for guidance:

If you want to find multiple elements with similar attributes, you can use the following code:

twoButtons = locate_elements_by_css_selector("button[class='btn bid-hotel-btn']")

If the class attribute is not unique, you can combine other attributes to make it more specific. For example:

twoButtons = locate_elements_by_css_selector("button[class='btn bid-hotel-btn'][type='button']")

The variable twoButtons will contain two web elements representing the buttons you intend to click on.

You can then interact with each button in the twoButtons list like so:

twoButtons[0].click() // Click the first button
twoButtons[1].click() // Click the second button

When I mention "first" and "second" above, I am referring to the order in which the buttons appear in their HTML structure. The First button will come before the second button.

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

Even in report-only mode, Content Security Policy effectively blocks the execution of inline scripts

Currently, I have implemented a Content Security Policy (CSP) in 'Content-Security-Policy-Report-Only' mode with a specified report-uri. There is an inline JavaScript code running on the page that the CSP restricts. My initial expectation was tha ...

Sequenced thunk execution during rendering

My application automatically retrieves user information upon rendering. When the app is first launched, it uses the getUserInformation() function to fetch this data. Users do not need to log in manually as the app operates within the company's interna ...

Generating CoffeeScript script that calls a class

It seems like I'm facing some difficulties here and not making much progress. I wrote a class using Coffeescript: # CoffeeScript App= Title:"" TopMenu:[] AddTopMenu:(title,count,icon)-> Record= ...

Steps to resolve the "cannot get product" error on Heroku after submitting a value

view the image description hereI have been working on a MERN full stack project and everything was functioning correctly in my local environment. However, upon deploying it to Heroku, I encountered an error which is displayed below. All other APIs are fu ...

Alter the URL and CSS upon clicking an element

Currently, I am faced with a challenge on my website where I have multiple pages that utilize PHP to include content and jQuery to toggle the display of said content by adjusting CSS properties. However, I am encountering difficulty in implementing this ...

Create a dynamic progress bar that integrates seamlessly with the ajaxupload functionality for a smoother video uploading

Is there a way to display an accurate time estimate alongside a visually appealing progress bar during video uploads? Currently, I am utilizing the ajaxupload javascript library in conjunction with PHP CodeIgniter for server-side functionality. ...

Developing a Fresh Settings Tab and Vue Module in Laravel Spark

When working with Laravel Spark, you'll find that most settings panels come with a corresponding Vue JS component that is called upon using a custom tab. <spark-create-tests :user="user" inline-template> <div> </div> </sp ...

Automated logout feature will be enabled if no user interaction is detected, prompting a notification dialog box

Here is my working script that I found on this site. After a period of idle time, an alert message will pop up and direct the user to a specific page. However, instead of just the alert message, I would like to implement a dialog box where the user can ch ...

How can I run Selenium in headless mode with a saved browser session in Python?

Looking to find a way around the web.whatsapp.com QR scan page? Here's the code I've been working with: options = webdriver.ChromeOptions(); options.add_argument('--user-data-dir=./User_Data') driver = webdriver.Chrome(options=options) ...

JavaScript - asynchronous XMLHttpRequest with increment operation

Hello, it's my debut on SO, although I've been a regular user for quite some time and found all the answers I needed here...until now. I'm currently grappling with an intricate issue involving XHR. While I consider myself a bit of a JS newb ...

Retrieving data with JQuery when encountering an XHR error

When I send a jQuery AJAX request and receive a successful response, I am able to retrieve my JSON data. However, if the response code is anything other than 200, I am unable to access the data in the jQuery callback function. This data is crucial as it co ...

Loading images in advance using jCarousel

I need help with preloading images on a jCarousel that loads a JSON feed and generates necessary HTML. Can anyone suggest a way to accomplish this task efficiently? $(".banner ul").jcarousel({ itemLoadCallback:loadTopBanner, auto: 6, wrap: ...

Troubleshooting problem with sorting in Angular 4 material header

Using Angular 4 material for a table has presented me with two issues: 1. When sorting a table, it displays the description of the sorting order in the header. I would like to remove this. https://i.sstatic.net/5bHFO.png It displays "Sorted by ascending o ...

Storing user data in node.js using the express-sessionTo save user data in

Using express and express-session with mysql on nodeJS has been successful for me. I managed to set up a cookie and session as well. Take a look at my code: app.use(cookieParser('3CCC4ACD-6ED1-4844-9217-82131BDCB239')); session({resave: true, s ...

Typescript: The type 'X' does not correspond with the signature '(prevState: undefined): undefined' in any way

My React Native app, which is written in TypeScript, has been giving me a hard time with an error lately. The issue revolves around a Searchable List feature. This list starts off with an Array of values and gets updated when users type into a search bar. ...

Updating div with specific class based on selected values

I have utilized Angular to display content based on selected values from a dropdown menu. Now, I am seeking a solution to dynamically add a CSS class to a div when specific values are chosen. In other words, I want to apply certain styling to the div base ...

.toggle function malfunctioning

Upon page load, I have a script that toggles the County menu. The goal is to hide the county menu if any country other than "United Kingdom" is selected on page load. If the user selects another country after the page has loaded, there is an issue where ...

When building websites, pages, or applications with React, how do you determine the best choice between JavaScript, TypeScript, or JavaScriptXML?

After completing my portfolio and an eCommerce website design using Figma, I started learning React and Tailwind with Vite. I'm comfortable with basic JavaScript but now I want to understand the differences between .js, .jsx, and .ts files when workin ...

Can you invoke a class within a switch statement in C#?

This is an example of C# selenium webdriver automation. A script has been developed in C# to run and generate a report on a webpage. There are 10 different links that the user can select to create a report within the page. To replicate this for 10 other li ...

Press the Export to Excel button through Selenium WebDriver

I am currently facing a challenge where I need to interact with an Export to Excel button using Selenium Webdriver. The issue is that the button is actually a Flash button and the HTML code for it has been commented out. As a result, Selenium is unable to ...