Tips on verifying the presence of an element within the HTML with Selenium

My inquiry revolves around a specific process. I am tasked with locating an element on a webpage using its class, extracting and displaying its text, then splitting it into parts using the split() function. However, I encounter an error when the specified element is not found, resulting in failure to parse.

Here is the code snippet:

spans = driver.find_elements(By.XPATH, "//span[@class='ipsContained ipsType_break']")

for span in spans:
    atag = span.find_element(By.XPATH, ".//a")
    print(atag.get_attribute('href'))
    urlik = atag.get_attribute('href')
    driver.get(url=urlik)
    time.sleep(2)
    try:
        urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text
        for page_number in range(int(urla.split()[3])):
            page_number += 1
            driver.get(url=urlik + f"page/{page_number}")
            time.sleep(2)
            imgs = driver.find_elements(By.CLASS_NAME, "cGalleryPatchwork_image")
            for i in imgs:
                driver.execute_script("arguments[0].scrollIntoView(true);", i)
                time.sleep(0.2)
                print(i.get_attribute("src"))
    except NoSuchElementException:
        print("Element not found")

The particular section requiring verification is as follows:

urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text

Answer №1

If you want to locate an element on a webpage by its class and extract the text content regardless of whether the element exists or not, you can use a try-except block to handle any potential NoSuchElementException. Here is an example implementation:

driver.get(url=urlik)
time.sleep(2)
try:
    urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump").text
    for page_number in range(int(urla.split()[3])):
        page_number = page_number + 1
        driver.get(url=urlik + f"page/{page_number}")
        time.sleep(2)
        imgs = driver.find_elements(By.CLASS_NAME, "cGalleryPatchwork_image")
        for i in imgs:
            driver.execute_script("arguments[0].scrollIntoView(true);", i)
            time.sleep(0.2)
            print(i.get_attribute("src"))
except NoSuchElementException:
    print("Element is not present")

Answer №2

Instead of

urla = driver.find_element(By.CLASS_NAME, "ipsPagination_pageJump")

Try this approach instead

urla = driver.find_elements(By.CLASS_NAME, "ipsPagination_pageJump")
if urla:
    urla[0].text

The find_elements method will give you a list of web elements that match the given criteria.
If there are matching elements, the urla list will be non-empty and interpreted as True in Python.
If no matching elements are found, the urla list will be empty and interpreted as False in Python.

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

Finding HTML source with regular expressions in Python: A guide

I've been wracking my brain trying to crack this problem. Although the numbers and names in this scenario are made up, the concept is the same. For instance, when I go to a link like 'https://graph.facebook.com/123' This is what I get bac ...

Ways to locate the value of x within a string using Python

I need help with finding a specific string within a larger string for my IRC bot project. I want the bot to greet users using a command like "!greet Greg" and have the bot respond "Hi, Greg!". The name after "greet" should be dynamic so that if I wrote !gr ...

What is the best way to format a string containing prices using Python?

In my database, there is a field in the record table that contains prices in string format. The prices are unformatted and may vary: $12,000.50 $3.50 From $3.50 to $12,000.50 My goal is to extract the numerical value from these strings by removing all "$ ...

Is it possible to integrate Chrome browser into a python QT GUI?

Currently, I am working on a Python GUI that leverages Selenium to open a Chrome window. I was curious if there is a way within the PyQT GUI to embed the Chrome browser's window, essentially combining them into one interface instead of having separate ...

Ways to create a loop that continues running until a specific condition becomes true

How can I make the code repeat until a certain condition is met? I want the code to keep repeating until it is true In my script, I have a condition that checks for a specific color using pyautogui. If the color matches, I want the program to click on ce ...

Move the primary window to the front in PySide

The situation: Here's the deal - I've got a total of 3 windows along with 1 window manager file. The window manager kicks off the main window and you can open other windows from there. Everything seems to be going smoothly so far. However, all t ...

What are the steps for adjusting the size of a frame using the cursor located inside it?

Is there a way to dynamically change the size of a QFrame based on the cursor position? For example: If the default width is set to 50, when the cursor hovers over the frame, the width should increase to 100. When the cursor moves outside of the frame, t ...

Is the presence of the selenium webdriver being registered?

Is there a method to detect if a website can tell I am using a bot while running Selenium WebDriver in Python or Puppeteer in JavaScript? Are there any resources that indicate whether a bot test would be failed, such as Cloudflare or Captcha? Appreciate a ...

How to eliminate excess whitespace surrounding patches in Matplotlib

Looking at the image provided (1), I'm struggling to find a way to eliminate the excessive white space surrounding the hexagonal grid. Despite attempting various methods such as tight_layout, the issue persists. https://i.stack.imgur.com/iFeQq.png T ...

Top method for importing time series data from a CSV file and creating a visualization using Python

One task that I frequently encounter is parsing a CSV file containing time series data and then creating a graph to visualize it. The data in the CSV file may not be sorted, could contain gaps, and each series may have different start and end dates. For e ...

What is the best way to isolate specific strings that begin with a symbol from a text file using Python?

Is there a way to extract words that begin with the symbol '$' from a text file? Here is an example of the text file (ascii): @ExtendedAttr = nvp_add(@ExtendedAttr, "severity", $severity, "description", $description, "eventID", $eventID, ...

Having difficulty bringing in a Python function into Jupyter notebook

My Jupyter notebook is equipped with Python 3.5, which I utilize for data analysis from a simulation coded in Python. Upon running the simulation in the initial cell using: %run control.py I encountered the following error: > ImportError ...

Is there a way to disable the printer feature?

from tkinter import * root = Tk() # This function is designed to control the button click status def check_button_status(is_clicked): return is_clicked # The printer function displays numbers fro ...

Exploring the dynamic capabilities of Pandas with the use of .cut and

Having both dataframes, here is the first one: x = pd.Series( ["(-20, -10]", "(-140, -130]", "(0, 10]"], dtype = "category") y = pd.Series( ["(0, 50]", "(100, 150]", "(-50, 0]"], dtype = "category") df_xyz = pd.DataFrame({'x_bin': x, 'y_bi ...

Achieve visibility on the latest window using Selenium WebDriver in Python

After successfully clicking on a button that triggers the opening of a new browser window with Selenium Webdriver in Python, I'm faced with the challenge of shifting the focus to this newly opened window. Despite thorough internet searches, the lack o ...

I am interested in implementing a variable to define rows within a <tr> element in an HTML table

I am facing an issue with finding specific rows in a table by using a variable like /tr[i]/ where i represents the row I want to access. When I hardcode the element in my code, it works perfectly: driver.find_element_by_xpath("//tbody[@class='sample& ...

Verify the presence of an element by using the WebDriver findElements() method along with the

Is there a way to determine whether an element exists on a webpage or not? I came across this WebDriver: check if an element exists? thread, but I'm curious as to why we can't just use the findElements().isEmpty method instead? It seems like ...

Automate sign-in across various browsers using Selenium

For the past 48 hours, I've been struggling with a problem that I can't seem to solve. The task at hand involves logging into multiple browsers on various machines for the same website. It's a time-consuming process, so I've decided to ...

Using Python Webdriver to Execute JavaScript File and Passing Arguments to Functions

How can I execute a JavaScript function and pass arguments to it? value = driver.execute_script(open("path/file.js").read()) I have successfully executed the file, but I am unsure of how to pass arguments to the function within it. Any suggestions would ...

Is there a way to only retrieve the title and artist information from the data?

I am currently working on extracting the titles and artist from a JSON page. While I can successfully retrieve all the entries, I'm having some difficulty in isolating just the title and artist information. Below is the snippet of code I am using to a ...