How can I determine the specific quantity of XPATH links with unique identifiers in Selenium?

Seeking automation with Python3 and selenium to streamline searches on a public information site. The process involves entering a person's name, selecting the desired spelling (with or without accents), navigating through a list of lawsuits, and accessing individual case pages.

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException, NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
import re

Specify the search name

name = 'JOSE ROBERTO ARRUDA'

Set up paths, starting link for search, and an empty list to store data

firefoxPath="/home/abraji/Documentos/Code/geckodriver"
link = 'https://ww2.stj.jus.br/processo/pesquisa/?aplicacao=processos.ea'
processos = []

Initialize driver and navigate to the initial search page

driver = webdriver.Firefox(executable_path=firefoxPath)
driver.get(link)

Input text and initiate the search

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#idParteNome'))).click()
time.sleep(1)
driver.find_element_by_xpath('//*[@id="idParteNome"]').send_keys(name)
time.sleep(6)
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#idBotaoPesquisarFormularioExtendido'))).click() 

Select all possible spellings for the search

WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#idBotaoMarcarTodos'))).click()
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#idBotaoPesquisarMarcados'))).click()
time.sleep(1)
... ... ...

Answer №1

Here is the code snippet for the "Capture routine":

wait = WebDriverWait(driver, 20)

#...    

while True:
    links = wait.until(EC.presence_of_all_elements_located((By.XPATH, "//span[contains(@class,'classSpanNumeroRegistro')]")))
    print("Number of links:", len(links))
    for i in range(1, len(links) + 1):
        # Access the direct link and click
        .until(EC.element_to_be_clickable((By.XPATH, f"(//span[contains(@class,'classSpanNumeroRegistro')])[{i}]//a"))).click()

        # Run tests to gather data
        try:
            unique_num = driver.find_element_by_xpath('//*[@id="idProcessoDetalhesBloco1"]/div[6]/span[2]/a').text
        except NoSuchElementException:
            unique_num = "no_unique_number_found"

        try:
            proc_name = driver.find_element_by_xpath('//*[@id="idSpanClasseDescricao"]').text
        except NoSuchElementException:
            proc_name = "name_not_found"

        try:
            aut_date = driver.find_element_by_xpath('//*[@id="idProcessoDetalhesBloco1"]/div[5]/span[2]').text
        except NoSuchElementException:
            aut_date = "date_not_found"

        # Fill dictionary and list
        dictionary = {"unique_num": unique_num,
                      "proc_name": proc_name,
                      "aut_date": aut_date
                      }
        processes.append(dictionary)

        # Go back a page to click on next process
        driver.execute_script("window.history.go(-1)")

    # wait.until(EC.presence_of_element_located((By.CLASS_NAME, "classSpanPaginacaoImagensDireita")))
    next_page = driver.find_elements_by_css_selector(".classSpanPaginacaoProximaPagina")
    if len(next_page) == 0:
        break
    next_page[0].click()

Answer №2

If you want to continue running the loop until the next button appears on the screen, you can use this logic:

try:
    next_button = driver.find_element_by_class_name('classSpanPaginacaoProximaPagina')
    if(next_button.is_displayed()):
        next_button.click()

except NoSuchElementException:
     print('Next page button not found')  

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

Python is the way to go for clicking on a link

I have a piece of HTML and I am in need of a Python script using selenium to click on the element "Odhlásit se". <div class="no-top-bar-right"> <ul class="vertical medium-horizontal menu" dat ...

What is the best way to serialize data when a writer subclass of iobase.write is responsible for writing records to a local server, and the writer process is distributed

The information in the "Custom Sources and Sinks (Python)" document (https://cloud.google.com/dataflow/model/custom-io-python) explains how the writing process involves multiple workers. How does the "finalize_write" method of the custom Sink handle worke ...

What could be causing my div elements to not appear on the page?

Hey there, this is my debut post here so bear with me on the formatting. I'm currently working on a project and encountering some hurdles with my divs. Even though I've left space for them, I just can't seem to get my divs (or cards) to disp ...

What is the best way to dynamically change the JSON-LD Script for the Schema?

Below is the script in question. Please read through it carefully. <script type="application/ld+json"> { "@context": "http://schema.org/", "@type": "Product", "name": "Bat, &q ...

Setting a fixed data value within a div for subsequent retrieval through a function

I found a helpful example that demonstrates how to convert numbers into words. You can check it out here. The function for converting numbers into words is implemented in the following HTML code: <input type="text" name="number" placeholder="Number OR ...

The asyncio add_signal_handler function is failing to capture the sigint and sigterm signals

I'm currently facing a challenge with debugging an issue in my asyncio project. My goal is to ensure it shuts down smoothly. import asyncio import signal async def clean_loop(signal, loop): print("something") tasks = [t for t ...

Issue with requests due to MissingSchema exception

I have a unique person class that provides detailed information about individuals, as shown below: Datatracker.py class @dataclass class Person: resource_uri: str id: int name: str name_from_draft: str ascii: str ascii_short: Opti ...

What is the reason behind the change in style hierarchy when using ':last-child'?

I found myself in a confusing situation where using :last-child is affecting how parent classes are being applied. The task at hand is to style the last element in a list of elements. However, upon using :last-child, the priority of styles shifts and one ...

What is the best way to eliminate the gap between header, content, and footer sections when in mobile view?

This issue seems to only occur in mobile mode, where the blueviolet line is coming from the background color. https://i.stack.imgur.com/VBhIp.png link to the image body { background-color: blueviolet; } header, main, footer { background-color: #ff ...

When trying to load the JSON data saved in a JSON file, I encounter an error: JSONDecodeError: Expecting value at line 1, column 1

In this scenario, I am experimenting with saving data in json format to a json file, but encountering issues when trying to load it back. import json # Python objects can be stored in json format value = [ ['sentence one', {'en ...

Keeping information saved locally until an internet connection is established

I have a vision to develop a Web app focused on receiving feedback, but the challenge lies in the fact that it will be utilized on a device without an internet connection. The plan is for it to save any user input offline until connectivity is restored. Th ...

Searching for a hyperlink WebElement with Java and Selenium: Finding the way to locate it

I have been attempting to click on the "sign out" link in Gmail, but my console keeps stating that it cannot find the element. Here is the code I'm using. Thank you! @FindBy(linkText="Sign out") WebElement logoutLink; This is the HTML: view image de ...

Is there a way to extract the unicode/hex representation of a symbol from HTML using JavaScript or jQuery?

Imagine you have an element like this... <math xmlns="http://www.w3.org/1998/Math/MathML"> <mo class="symbol">α</mo> </math> Is there a method to retrieve the Unicode/hex value of alpha α, which is &#x03B1, using JavaScrip ...

How to override an event in JavaScript when a specific value is entered

I am looking to implement a system with complex conditions. The goal is to have an alert appear when a value is inputted and the button is clicked. First, a confirmation message Have you input ? should be displayed, followed by clicked with input. If no va ...

Handling alert, confirm, and popup events in C# using Selenium

Despite extensive research, I have yet to find a solution that addresses the specific issue at hand. Our team is utilizing Selenium with C#. The main problem we are facing is our inability to control alerts, which may be due to their quick disappearance: ...

Tensorflow encountered an error in Python indicating that input_1:0 is being both fed and fetched, resulting in an InvalidArgumentError

I am attempting to execute a script that calculates the number of falls and non-falls in human fall detection, but I keep encountering an error: input_1:0 is both fed and fetch. I have tried running it independently without success. from keras.models ...

Combining Python, Neo4j, and JSON to uniquely create a new NODE for each user

I am looking to create a Python script that can accomplish the following tasks: Load a JSON file containing user data Create a node for each user in the JSON file ## Sample User Data {'UserName': 'lolipop', 'UserId': '5 ...

The Selenium Internet Explorer driver is having difficulty locating the accurate webpage source

Currently, I am faced with a challenge on my login page that redirects to a different page where I need to extract data from an element using Selenium. While running the code locally in Eclipse with the IE driver, I encountered an issue where the page sour ...

Troubleshooting issue with setting browser version using Selenium Add Additional Capability in .NET Framework not resolving

I am currently working on testing my program across various browser versions. To begin, I opted to use ChromeDriver by implementing the following code: using OpenQA.Selenium.Chrome; ChromeOptions Options = new ChromeOptions(); Options.PlatformName = " ...

The enchanting dance of words ceased as Poetry ran off with Worker.py, only to realize that the file b'/snap/bin/worker.py' was nowhere to be found in the directory

After running the command below in the same directory as the file worker.py: poetry run worker.py I encountered the following error in the terminal: me@LAPTOP-G1DAPU88:~/.ssh/workers-python/workers/composite_key/compositekey$ poetry run worker.py File ...