Is there a way to determine the quantity of divs with a particular class through selenium?

I have created a selenium Bot that can automatically play the addictive game cookie clicker.

My goal is to access the list of buildings in the game as they get unlocked during gameplay. I want the Bot to be aware of all unlocked buildings so it can make informed decisions when purchasing new ones. The game updates building statuses by changing the class names from "product locked disabled" to "product unlocked disabled."

I need to count how many divs have the "unlocked" name within their classes, but the issue is that the inner HTML of these divs is empty. This results in the find_elements_by_class_name() method returning an empty list. Can anyone suggest a way for me to count the divs with a specific class name?

Answer №1

When using selenium's find_elements_by_class_name, it only accepts one class name according to this source. However, if the desired div has multiple classes like product, locked/unlocked, and disabled, it is recommended to utilize CSS selectors (or XPATHs) instead.

To locate a div with the classes-

  • product
  • locked
  • disabled when utilizing CSS selectors, you should use-
driver.find_elements_by_css_selector('div.product.locked.disabled')

To find a div with the classes-

  • product
  • unlocked
  • disabled using CSS selectors, you should use-
driver.find_elements_by_css_selector('div.product.unlocked.disabled')

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

What is the best way to make a python script repeat a set number of times continuously?

To repeat a Python script 10 times, I encountered an intentional error that halts the process. This occurs because the script interacts with a website where a random number of questions are generated, up to a maximum of 7. To handle this variability and pr ...

Error: You need to have `accelerate` installed in order to use the `Trainer` with `PyTorch` package

A few months ago, I developed a code to train an NER model which was functioning perfectly. However, when I tried running the same code recently, I encountered this error: ImportError: Using the `Trainer` with `PyTorch` requires `accelerate`: Run `pip inst ...

Is it considered Pythonic to use expressions of boolean type in the form of 'bool = (boolean expression

Consider the code snippet below: is_valid = command == "B" or can_move = move in legal_moves() Both statements follow the format: bool = (boolean expression) I often find myself writing such statements and I'm starting to question if this is not ...

Techniques for capturing Django's output from an Ajax request

I've been trying to utilize Ajax for converting my form data to JSON and sending it to my Django view. However, I'm encountering an issue where after successful processing in the view, I am returning a template response with some context data tha ...

Is there a way for me to navigate back to the active driver window once my script has finished running?

Utilizing Selenium WebDriver alongside Chrome WebDriver, my script1 captures a URL from the driver.get(" ...") method and performs various tasks like web scraping (such as clicking buttons, extracting information, and logging into a website). Af ...

When executing in the local development environment, Selenium tests encounter errors and reach the error page with a connection issue (about:neterror?e=connection). However, the same tests fail

Encountering a perplexing issue with a Selenium test that fails in Travis CI but runs smoothly in the local development environment. The error message indicates that Firefox is unable to establish a connection to the server at localhost. To troubleshoot th ...

Tips for confirming all the elements on a single page with Data Table

Seeking guidance on how to locate every element within the same page and programmatically confirm their visibility using a Data table in Selenium, Java, or Cucumber. Consider a hypothetical situation like this Scenario: Validate all elements in the xyz p ...

Unable to debug json.loads() code using pdb

I am curious to understand the inner workings of converting a JSON string to a Python dictionary using json.loads() For example: import json s = '{"a": 1, "b": 2}' # input json string d = json.loads(s) # output dictionary object To dive de ...

Selenium: The parent window automatically closes when a child window pops up

I'm encountering a challenge with switching to a child window in a specific scenario Scenario: 1. On the Login Page, I need to enter my user id and password. 2. After entering the credentials, I click on the submit button. 3. The system then opens a ...

Python Package limit exceeded when uploading Lambda Function

My python code has several dependencies that need to be included: import json import pydicom from pydicom.dataset import Dataset, FileDataset from pydicom.uid import ImplicitVRLittleEndian import numpy as np from PIL import Image import cv2 import datetime ...

Visual Studio build is disrupted by the presence of node_modules folder

I am currently in the process of integrating Angular (v4) into an existing ASP.NET MVC 4 application. Within this application, there is a project that includes Selenium Web Driver along with a web.config file. node_modules\selenium-webdriver\lib ...

Ways to perform a right-click in a selenium test

In my current project with seam, I have been using the recursiveTreeNodesAdaptor. I am trying to allow users to add child nodes to this tree via a contextMenu. My goal is for the context menu to open when the user right-clicks on a node in the tree, and if ...

Creating anchor tags with href attribute around a specific substring within a string using Django

Presently, I am utilizing regex to locate a specific substring within a given string and then wrapping it in anchor tags with an href attribute within a jinja template. pattern = re.compile(rf"({substring})",re.IGNORECASE); anchoredString = mark_safe(re.s ...

Error: The method sort_values() is missing a necessary argument: "by"

I am working with a dataset that looks like this df2=df1.head(10) genres imdb_score 0 Action 6.239896 1 Adventure 6.441170 2 Animation 6.576033 3 Biography 7.150171 4 Comedy 6.195246 5 Crime 6.564792 6 Documentary 7.180165 7 Dra ...

Miscalculation in Python code

Currently, I am working on incorporating an efficient LCM calculation algorithm that can handle very large numbers. The LCM is computed using the following formula: LCM(A, B) = (A * B) / GCD(A, B) Here, A and B represent two input values. For example: ...

Is it necessary to hover over the menu in order to access the clickable element?

Be sure to hover over the menu first before trying to click on the element! The element is currently not visible and cannot be interacted with (WARNING: The server did not provide any stacktrace information) error on Selenium I have attempted the followi ...

The cross validation estimator encountered an invalid parameter, causing a value error

While working on parameter tuning for an SVM model, I encountered an issue with the gamma value. Despite trying three different values for gamma, I received an error stating that the parameter is not valid for the estimator: Invalid parameter gamma for e ...

How to convert the Unicode characters in Python for the string 'u05d9u05d7u05e4u05d9u05dd'?

Received a Json object from a URL with values formatted like this: title:'\u05d9\u05d7\u05e4\u05d9\u05dd' Attempting to convert these values into readable text, but struggling with them being interpreted as literal strin ...

What is the best method to locate the Excel value in Python based on row and column names?

I'm currently using Python to work with .xlsx files and relying on openpyxl library for assistance. If I know the column name and row number, is it possible to retrieve the value in that cell from the xlsx file? For instance: column - P row - 3 ...

Guide on utilizing the Selenium dropdown selector

Having trouble with the selenium selector, as I keep encountering this error message when attempting to use it. The command "web.find_element("id",'ticket-quantity-selector-756706789')" doesn't seem to work in my terminal. Any help would be ...