What is the best way to retrieve the content of a specific class in Selenium WebDriver?

Python Code :

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

options = webdriver.ChromeOptions()
options.add_argument(r"--user-data-dir=C:\Users\bji\AppData\Local\Google\Chrome\User Data") 
options.add_argument(r'--profile-directory=Default')
driver = webdriver.Chrome(executable_path=r'C:\Users\bji\Downloads\chromedriver_win32(1)\chromedriver.exe', chrome_options=options)

driver.get('wanted website')

content = driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div/div[2]/ul/li[1]/a/div[3]/div[2]/div/p')

if ( content == 24 ) :
    content.click()

This is the corresponding HTML code :

<p data-v-d31307be="" class="tw-ml-2 text__green">24</p>

Answer №1

A convenient way to retrieve text using Selenium is by utilizing the .text method.

content = driver.find_element_by_xpath('//*[@id="__layout"]/div/div[2]/div[1]/div[1]/div/div[2]/ul/li[1]/a/div[3]/div[2]/div/p')
if content.text == "24":
   content.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

Alignment issue with React JSX background position; image not centered on the right in Material UI Grid item

I've recently completed a single-page website portfolio, but I'm encountering an issue with setting the background position of the home screen to center right. You can view my site here: Here is the link to my repository: https://github.com/Bolm ...

Instructions for creating a background within a container and positioning text in the center of the image utilizing HTML and CSS

I am currently learning HTML, CSS, and JavaScript. I am in the process of building my very first website, but I have encountered a problem. I am struggling to change the background color within the container beneath the images to a specific color (#82b5cf) ...

The Tabbed Panel Bug: Incorrect HTML ID Causing Tab Change Issues in Bootstrap

I've been working on a tabbed panel feature for my website. However, I've run into an issue where clicking on the second, third, and other tabs doesn't change the content displayed - it always stays the same. I'm pretty sure I wrote th ...

Adding complex JSON format to an HTML table involves formatting the data correctly and then using

Utilizing AJAX, I fetched a JSON response and am now looking to map the JSON data into an HTML table structured like this: { "records": [{ "type_id": 000001, "type_desc": "AAAAAA", "type_createby": "Adam" }, { "type ...

My goal is to generate four HTML buttons that trigger specific functions: addition, subtraction, multiplication, and division

I am currently learning JavaScript, and I am facing some challenges with my assignment. The task is to create four buttons in HTML that trigger different functions - addition, subtraction, multiplication, and division. The goal is for the user to input two ...

I encountered a Null Pointer Exception while trying to retrieve information from the Excel spreadsheet

Code: public class ContactFormTest { WebDriver driver; @BeforeMethod public void setUp() { System.setProperty("webdriver.chrome.driver", "Driver Path"); driver=new ChromeDriver(); driver.get("URL"); driver.manage().deleteAllCookies(); driver.manage().wind ...

Error encountered when attempting to generate a hyperlink

Whenever I try to assign hyperlinks to each image name, I keep encountering an undefined imgitem error in the hyperlink(s). Could it be that I am incorrectly placing the <a> tags? echo '<td width="11%" class="imagetd">'; if (empty($a ...

Every time I attempt to insert a background image into a div using jQuery, I am consistently faced with a 404 error message

When I hit enter in my search bar, a new div is created each time. However, I am struggling to assign a background image to the created div as I keep receiving a 404 error in the console. Below is the code snippet I'm working with: function appendToD ...

background gradient coloring and image blending

I have created a special class that includes a gradient background color: .banner { background: -moz-linear-gradient(center top , #75A319 0%, #9FCC1D 100%) repeat scroll 0 0 #9FCC1D; } Additionally, I have defined another class that adds a background ...

JavaScript - Unable to Modify Select Value with Event Listener

It's interesting how I can change the selected option in a dropdown list using JavaScript without any issues. However, when I try to do the same thing within an event listener, the option does not seem to change. Here's the HTML code: <select ...

The description in the lower design must not be on the same line or at the same height as the element

I'm facing an issue with CSS regarding achieving the same height. I have a list of designs, each with a description at the bottom. While I've successfully aligned the designs vertically between portrait and landscape orientations, the problem ari ...

Convert Python strings into HTML JavaScript blocks using Jinja2

Having trouble passing a string to an HTML page in the "<script>" block. I am currently using Python, Flask, and Jinja2. Python code: def foo(): return myString #"[{title: 'Treino 7-Corrida',start: '2015-12-08',color: '#d ...

Exploring the contrast between Selenium's --headless and -silent modes

Whenever I use the selenium option argument on my personal computer, whether in --headerless or --silent modes, everything runs smoothly. However, when attempting to run it on a client's device, it throws an error with a strange code. I've even t ...

What are the steps to testing a webpage designed for Retina display?

I have designed a webpage optimized for retina display, but I don't own a monitor with that capability. Is there a simulation tool available to test websites on retina displays? Alternatively, are there non-Apple monitors comparable to Apple's ...

Dynamically insert textboxes into a form by leveraging the power of the Jade template engine

Looking for a solution to a simple requirement that doesn't seem to have a clear answer online. I need a combobox on my jade page that accepts numbers as input. When the user selects a number, I want the page to refresh and display that many textboxes ...

Internet Explorer (on mobile devices) does not display submenus when touched

Hello, I have a query about displaying a sublist on touch. I have created a sublist that is supposed to show up when you hover over it. You can view the demo here: @import url(//fonts.googleapis.com/css?family=Titillium+Web:400,200,300,600,700); body { ...

Tips for adjusting the scrollbar in Tailwind (next.js/react)

Having trouble changing the appearance of my scrollbar in a single page application using Tailwind (react/next). I've attempted to create custom CSS for the first div in my index file, like so: <div className="no-scroll"> <<< ...

Automatically changing the page's background color through animation upon loading

Similar Question: jQuery animate backgroundColor Can I make the background-color of a specific element change gradually when the page loads? I have a webpage with content like this: <ul> <li> Some stuff</li> <li> Some ...

Tips on presenting messages to users after clicking the submit button?

I am trying to extract data from a table. I am unsure if my current code is able to retrieve the value from the table. <script> function validateForm() { var x = document.forms["assessmentForm"]["perf_rating11"].value; var y = document.f ...

Using Selenium to interact with the button that triggers the ng-click event

I am currently working on automating a website using Selenium to download an Excel file. The HTML code for the button I need to click looks like this: <div class="row ng-scope" ng-if="!model.update_rota"> &l ...