Python Selenium clicking next button until the end

2024/10/3 6:36:23

This is a follow up question from my first question, and I am trying to scrape a website and have Selenium click on next (until it cant be clicked) and collect the results as well.

This is the html tag from the website with the button:

<div class="pager results-display"><span class="action-btn prev inactive" data-page="1">Prev</span><span class="action-btn inactive" data-page="1">1</span>  # Currently in page 1 thus inactive<span class="action-btn" data-page="2">2</span><span class="action-btn" data-page="3">3</span> <span class="action-btn" data-page="4">4</span><span class="action-btn" data-page="5">5</span><span class="action-btn" data-page="6">6</span><span class="action-btn" data-page="7">7</span><span class="action-btn" data-page="8">8</span> <span class="action-btn" data-page="9">9</span><span class="action-btn" data-page="10">10</span><span class="action-btn" data-page="11">11</span> <span class="action-btn" data-page="12">12</span><span class="action-btn" data-page="13">13</span><span class="action-btn" data-page="14">14</span><span class="action-btn next" data-page="2">Next</span>
</div>
<div class="no-results-display hidden" style="display: none;">No companies matched your search. Try again.</div>

I've tried this code:

elm = driver.find_element_by_name('pager results-display')
elm.click()

And I've also checked this question out, but still wasn't able to solve it.

Any thoughts?

Answer

pager results-display are actually two classes that belongs to the button's parent element. They aren't name attribute, and find_element_by_name gets only one name anyway. Try

elm = driver.find_element_by_class_name('next')
elm.click()

Notice that you might have to relocate the button each time if the DOM has changed after the click.

To click in loop as long as the button is active you can check if the buuton has class inactive

while True:elm = driver.find_element_by_class_name('next')if 'inactive' in elm.get_attribute('class'):break;elm.click()
https://en.xdnf.cn/q/70762.html

Related Q&A

How can i detect one word with speech recognition in Python

I know how to detect speech with Python but this question is more specific: How can I make Python listening for only one word and then returns True if Python could recognize the word.I know, I could ju…

Finding matching and nonmatching items in lists

Im pretty new to Python and am getting a little confused as to what you can and cant do with lists. I have two lists that I want to compare and return matching and nonmatching elements in a binary form…

How to obtain better results using NLTK pos tag

I am just learning nltk using Python. I tried doing pos_tag on various sentences. But the results obtained are not accurate. How can I improvise the results ?broke = NN flimsy = NN crap = NNAlso I am …

Pandas apply on rolling with multi-column output

I am working on a code that would apply a rolling window to a function that would return multiple columns. Input: Pandas Series Expected output: 3-column DataFrame def fun1(series, ):# Some calculation…

Exceptions for the whole class

Im writing a program in Python, and nearly every method im my class is written like this: def someMethod(self):try:#...except someException:#in case of exception, do something here#e.g display a dialog…

Getting live output from asyncio subprocess

Im trying to use Python asyncio subprocesses to start an interactive SSH session and automatically input the password. The actual use case doesnt matter but it helps illustrate my problem. This is my c…

multi language support in python script

I have a large python (2.7) script that reads data from a database and generate pictures in pdf format. My pictures have strings for labels, etc... Now I want to add a multi language support for the sc…

Add date tickers to a matplotlib/python chart

I have a question that sounds simple but its driving me mad for some days. I have a historical time series closed in two lists: the first list is containing prices, lets say P = [1, 1.5, 1.3 ...] while…

Python Selenium: Cant find element by xpath when browser is headless

Im attempting to log into a website using Python Selenium using the following code:import time from contextlib import contextmanager from selenium import webdriver from selenium.webdriver.chrome.option…

Reading large file in Spark issue - python

I have spark installed in local, with python, and when running the following code:data=sc.textFile(C:\\Users\\xxxx\\Desktop\\train.csv) data.first()I get the following error:---------------------------…