How to automate clicking multiple buttons on the same webpage using Selenium with Python

As a Python and Selenium novice utilizing chromedriver, I find myself in need of assistance. The task at hand involves a web page that is unfortunately restricted from being accessed externally. This particular webpage hosts approximately 15 buttons with the same class. Each button, when clicked, disappears. My objective is to create a script that will systematically click on each button individually until they have all vanished. Following this, the page should be refreshed and the process repeated for the subsequent 100 pages.

Answer №1

Unfortunately, I am unable to access your page in order to test the code provided. However, I can offer some guidance on how you might approach this issue.

One potential solution is to gather all the buttons on the page into a list and then create a loop to click on each button individually. Here is an example of what that code might look like:

# This will create a list containing all the buttons
buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']")

# Clicking loop
for button in buttons:
    button.click()

You may encounter bugs when using Selenium, so it's recommended to include a small delay between each click. You can experiment with adding sleep(0.5) within the loop or consider utilizing JavaScript for the clicks if necessary.

Answer №2

In my experience, I have found a solution that works well for me. After doing some research, I came across a similar question with an answer that helped me. Although I am still unsure of how exactly it works, I have noticed that using specific numbers for buttons is necessary. For example, when I tried using buttons[4].click(), it did not produce the desired result.

buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']")
buttons[2].click()

buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']")
buttons[3].click()

buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']")
buttons[5].click()

buttons = driver.find_elements_by_xpath("//*[@class='btn default check check green markAsChecked']")
buttons[7].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

A Guide to Harvesting Email Addresses with Python and Selenium

Is there a way to use selenium to extract the 'email' field from a specific box? I need to retrieve the value="[email protected]". Any suggestions on how this can be accomplished? Check out the website here: https://i.stack.imgur.com/KNXK0 ...

Using OpenCV to identify a text image and implement code to halt execution

I've encountered an issue with my code that checks multiple generated codes. It's supposed to stop when it finds the winning variant, but I'm struggling to make it halt at that point. Can someone help me solve this problem? Take a look. If ...

Selecting specific products in Selenium with just a click

Hi there, I've been honing my skills with selenium and encountered an issue while trying to click on a specific product. It seems like my CSS selector is causing some trouble here. Could someone take a look and assist me in resolving this? Thank you f ...

Unable to establish TCP port connection from an external host

Seeking assistance desperately! I've been grappling with this issue for hours now. I'm at my wit's end, exhausted from scouring every possible resource with still no solution in sight. The predicament I find myself in involves a Python scri ...

Tips on comparing two JSON and YAML files to ensure their contents are similar

The following content is extracted from a yaml file where the type field appears in both Persian and English, note that 'Type TypeB' is correct: - dedicatedip: 1.1.1.1 status: Active type: 'نوع اول' - dedicatedip: ...

Hiding Chrome browser during VBA web scraping in Excel: techniques for seamless code execution

Can anyone tell me if it's possible to use Chrome or Edge with Selenium in Excel VBA for web scraping without displaying the browser window like you can do with Internet Explorer by setting IE.Visible = False? Thanks! ...

Unable to "clean up" the HTML document

Struggling with utilizing tidy to produce clean html code. My attempt looks like this: tidy -i -utf8 output/page_1530597755.html -b -o test.html However, no output file is being created! Instead, there are numerous warnings and an error message popping u ...

What is the meaning of 'sleepymongoose' is not defined?

After following the steps to set up sleepy.mongoose, a mongoDB REST interface as outlined here, I encountered some issues. Using windows 8 with Python 3.3.2 and pymongo installed, everything seemed to be in order. The 'help('modules')' ...

What is the best way to extract information from a website that utilizes react.js in Python using Selenium?

Struggling with web scraping on a site that utilizes react.js and unsure of the root cause. Here is the HTML structure of the website: https://i.stack.imgur.com/v3urW.png My objective is to interact with the button marked with the class: play-pause-butto ...

Python project organization template

As I work on organizing my project, I've come across a challenge with module imports. My project structure involves importing module_1 functions into module_2 located within src_1. src --src_1 -- module_1 --__init__.py -- module_2 --__ini ...

Is there a way to launch several instances of chromedriver simultaneously using a for loop in Python with Selenium?

Is there a way to launch multiple instances of chromedriver using a for loop in Python with Selenium? I've been trying different approaches with the code below, but haven't had any luck so far. drivers=[driver1,driver2,driver3,driver4,driver5] f ...

Navigating back to the current page from a frame using Selenium Webdriver

What is the best method for returning to the main page from an iframe? For example: driver.SwitchTo.Frame(1); driver.SwitchTo().DefaultContent(); The above code snippet is not working. Does anyone have any alternative solutions to regain control? ...

Interact with a web page by clicking on a random location using Selenium 2 with Python bindings

My current challenge revolves around figuring out the best method to execute a left-click within a specific area of a web browser window. I am utilizing Selenium version selenium-2.44.0 along with Python 2.7 for this task. While my end goal is to be able t ...

Loop over rows in a CSV file using Pandas and then execute actions with Selenium

I have a CSV file that was created using Pandas. Here is the result of running the code below: test = pd.read_csv('order.csv', header=0) print(test.head()) 3 16258878505032 0 3 16258876670024 1 3 16258876899400 2 3 1 ...

Tips for arranging the angles in a polar plot in a clockwise direction with the starting angle set at 0 degrees on top

I am currently using matplotlib and numpy to generate a polar plot within my project. Below is a snippet of the code I've been working with: import numpy as np import matplotlib.pyplot as plt angle = np.arange(0, 360, 10, dtype=float) * np.pi / 180. ...

discord.ext.commands.errors.CommandInvokeError: An error occurred while executing the command: TypeError: wait_for() is missing the required 'event' argument

I am currently facing an issue on line 19 of my code. The error message says that there is a missing argument called 'event'. I have tried to troubleshoot this problem myself by referring to the discord.py documentation, but I couldn't find ...

Is it possible to integrate a Neo4j Graph Visualization running on a virtual machine into my flask user interface?

My current setup involves running Neo4j on an Azure Virtual Machine and accessing the graph visualization through a web browser. In addition, I have developed a UI using Python with Flask that is currently running locally. I am interested in embedding th ...

What is the solution for resolving the error "ValueError: tortoise.backends.sqlite.client.SqliteClient object was initialized in a separate Context"?

I'm currently developing a Discord bot and utilizing Tortoise ORM for data storage. I've encountered an issue while attempting to use transactions, leading to the persistent occurrence of the following error: Full type of the error is <class ...

What is the best way to centralize all of the loggers from different modules under a single parent?

While I appreciate and support the hierarchical structure of loggers with canonical module names, I am facing a challenge in bringing everything together at the top level. For example, my application utilizes package1.subpackage1.module1 and package2.sub ...

A Python list containing a variable as a reference

I have labeled my lists as layer1, layer2 ... sequentially. I am looking to utilize a variable called playerY to reference the specific numbered list when calling it. For example, if the value of playerY is 4, then I want to access the content of layer4. ...