Triggering the retrieval of complete HTML content by clicking a button in order to load extra elements using Selenium

My goal is to scrape a webpage and gather all the links present on it. The page displays 30 entries and to access the complete list, one must click on a "load all" button.

Below is the Python code snippet I'm currently using for this task:

from selenium import webdriver
from bs4 import BeautifulSoup

driver = webdriver.PhantomJS()
driver.get('http://www.christies.com/lotfinder/searchresults.aspx?&searchfrom=header&lid=1&entry=edgar%20degas&searchtype=p&action=paging&pg=all')

load_all_button = driver.find_element_by_css_selector('a.load-all')
load_all_button.click()

elem = driver.find_element_by_xpath("//*")
source_code = elem.get_attribute("outerHTML")
soup = BeautifulSoup(source_code, 'lxml')

url_list = []
for div in soup.find_all(class_='image-container'):
    for childdiv in div.find_all('a'):
        url_list.append(childdiv['href'])
print(url_list)

Provided below is the HTML code snippet of the "load all" button:

<div class="loadAllbtn">
    <a class="load-all" id="loadAllUpcomingPast" href="javascript:void(0);">Load all</a> 
</div>

Despite implementing the above code, I am still only able to extract the initial 30 links instead of the complete list. It appears that I may not be utilizing Selenium correctly and would appreciate any insights on what might be going wrong.

Up until now, I've been successful in setting up Selenium, installing Node JS, capturing and saving a screenshot to a file.

Answer №1

By selecting "Load all," you trigger an additional request to fetch all the items available. It is important to allow some time for the server to respond:

from selenium.webdriver.support.ui import WebDriverWait as wait

driver = webdriver.PhantomJS()
driver.get('http://www.christies.com/lotfinder/searchresults.aspx?&searchfrom=header&lid=1&entry=edgar%20degas&searchtype=p&action=paging&pg=all')

labtn = driver.find_element_by_css_selector('a.load-all')
labtn.click()

wait(driver, 15).until(lambda x: len(driver.find_elements_by_css_selector("div.detailscontainer")) > 30)

This piece of code enables you to wait for a maximum of 15 seconds until the number of items exceeds 30. At that point, you can extract the page source containing the complete list of items.

Just a reminder that you can simplify the code by using:

source_code = driver.page_source

Additionally, there's no need to resort to BeautifulSoup for extracting links to each item. You can achieve this with:

links = [link.get_attribute('href') for link in driver.find_elements_by_css_selector('div.image-container>a')]

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

Discovering changing text on a website using Python's Selenium WebDriver

Website myList = ['Artik'] How do I verify if the content of myList is visible on the mentioned website? <span class="ipo-TeamStack_TeamWrapper">Artik<span> This specific webelement showcases 'Artik' in the given websi ...

Trouble triggering hidden radio button in Angular 9 when clicked

I have implemented radio buttons within a div with the following CSS styles (to enable selection by clicking on the div): .plans-list { display: flex; justify-content: center; margin: 2rem 0; .plan { display: flex; margin: 0 0.5rem; p ...

Preventing Div items from rearranging during size transitions using toggleClass

When I click on an element with the ID "id", I use toggleClass to resize a div from 10px to 500px in width. This is done partly to show/hide content. However, the issue arises when the transition occurs and the contents of the div start rearranging, overfl ...

Unable to establish a hyperlink to specific section of page using MUI 5 Drawer

When attempting to link to a specific part of my first page upon clicking the Shop button in the navigation Drawer, nothing happens: https://i.stack.imgur.com/FUQCp.png This snippet shows the code for the MUI 5 Drawer component: <Drawer anch ...

What is the process for executing a right-click with a mouse in Robot Framework?

I am currently creating automated tests using Selenium in Robot Framework IDE (RIDE). I am trying to figure out how to simulate a right click on an element on the webpage and select an option from the context menu. Is there a specific library available fo ...

Issues with the CSS code causing the dropdown menu to malfunction

Having trouble getting my CSS to apply when creating a drop-down menu. I've set up UL and LI elements for the dropdown, but the styling is not rendering as expected. Check out the screenshot below for a look at how it currently appears using developer ...

Center the popover over the element

I am in the process of creating a resource map using SVGs. My goal is to display a popover in the center of the element when a user clicks on it. However, due to CSS rotation of the map, the traditional techniques such as the one mentioned here have not be ...

Platform designed to simplify integration of high-definition imagery and scalable vector illustrations on websites

In the ever-evolving world of technology, some clients support advanced features like svg while others do not. With the rise of high resolution devices such as iPhone 4+ and iPad 3rd gen., it has become crucial to deliver graphics that can meet these stand ...

Having trouble incorporating custom CSS into my Rails/Bootstrap project

I’m having trouble figuring out what I’m doing wrong. I’ve added custom files and tried to override the existing ones. application.css.scss @import 'bootstrap-sprockets'; @import 'bootstrap'; /* * This manifest file will be co ...

Is there a corresponding Python Selenium WebDriver command that mirrors the functionality of the Selenium IDE command 'openWindow'?

We are currently in the process of converting a selenium IDE script to Python WebDriver. One particular command that we're having trouble with is: [openWindow | http://mywebsite.com/index.php | window1]] Despite searching for an equivalent WebDriver ...

What is the importance of using time.sleep in Python Selenium Webdriver when locating elements by XPath?

Recently, I encountered a peculiar situation while attempting to scrape a webpage using Python selenium webdriver. It seems that when I use the find_element_by_xpath method without including time.sleep, I am unable to retrieve any information. However, as ...

What is the method for updating the value of the Sass variable in Angular 4?

Within the file app.component.scss, there is a variable named $color that has been set to 'red'. What steps can be taken within the file app.component.ts in order to access this variable and change its value to 'green'? ...

What is the best way to align the text in the center without centering the numbers in an HTML ordered list?

I am currently working on a small widget for a webpage that displays steps in reverse order. My plan is to use an ol element and adjust the value attribute on each li tag to make the numbering of the ordered list reversed. Everything seems to be going smoo ...

"Creating a new element caused the inline-block display to malfunction

Can someone explain why the createElement function is not maintaining inline-block whitespace between elements? Example problem First rectangle shows normal html string concatenation: var htmlString = '<div class='inline-block'...>&l ...

What methods can I use to customize the appearance of the acceptance label in a contact form created with

I am currently developing a WordPress website using the Contact Form 7 plugin. However, I am facing an issue with styling the "I accept" button and text when clicking on the "registrace" button. I would like to center them and create some proper margin aro ...

Use CSS to manipulate the dimensions of an overlay

Looking for a way to ensure that the height of your overlay matches the height of your carousel, even on smaller screen sizes? The HTML code below demonstrates how to combine simple HTML with a Bootstrap carousel featuring three images, along with an overl ...

The appearance of my website appears differently depending on the resolution

Just recently, I began exploring the world of HTML/CSS/JS and created a handy tool for my personal use. However, I encountered an issue when viewing it on different resolutions which caused some elements to look distorted. The tool I made allows me to inp ...

Choosing pseudo-elements with CSS styling rules

I have been utilizing the Brave browser to block online ads. However, certain websites have found a way to insert ads into their HTML on the server-side, bypassing my ad-blocking efforts in Brave. Currently, Brave only offers the ability to block elements ...

Can object-fit be preserved while applying a CSS transform?

Currently, I am developing a component that involves transitioning an image from a specific starting position and scale to an end position and scale in order to fill the screen. This transition is achieved through a CSS transform animation on translate and ...

Unable to toggle class feature

I have a trio of play buttons for a custom player setup, displayed like so: <div> <div class="gs-player"> <div id="gs1" onclick="play(309689093)" class="fa fa-3x fa-play"></div> </div> <div class="gs-player"> ...