Using Scrapy and Selenium to interact with dynamic elements on a webpage

When I crawl the page, I come across an element that looks like this.

<a class="a-declarative" href="javascript:void(0)" 
data-action="a-expander-toggle" 
data-a-expander-toggle='{"allowLinkDefault":true, 
"expand_prompt":"See more", "collapse_prompt":"See less"}'>

<i class="a-icon a-icon-extender-expand"></i>
<span class="a-expander-prompt">See more</span>
</a>

In my scrapy spider, I am trying to click on the "See more" text using Selenium and then parse the response.

seemore = response.xpath('//a[contains(@data-action,"a-expander-toggle")]')

seemore.click()

However, I encounter an error when I try to click on seemore. When I log the xpath selector, this is the output I get.

# Log the "see more" link 
self.log(response.xpath('//a[contains(@data-action,"a-expander-toggle")]'))

# Log output
[<Selector xpath='//a[contains(@data-action,"a-expander-toggle")]' 
data=u'<a href="javascript:void(0)" data-action'>]

It seems like the link is not being read correctly. Can someone confirm if my xpath is correct for selecting this type of link?

Answer №1

A suggestion provided by Valdir Stumm Junior is to ensure that you invoke the click function as a webdriver element. While it may not be the most graceful solution, the following snippet of code (which has not been tested) can serve as a starting point for your situation:

from selenium.common.exceptions import NoSuchElementException

...

def scrape_page(self, response):
    self.driver.get(response.url)
    try:
        seemore = driver.find_element_by_class_name('a-expander-prompt')
    except NoSuchElementException:
        # Decide how you want to handle this scenario (e.g. breaking the loop, logging the occurrence)

    seemore.click()

For more information on accessing selenium elements, refer to:

https://selenium-python.readthedocs.io/locating-elements.html

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

jQuery Mobile dual range slider functioning but experiencing some glitches

I successfully created a dual range slider by stacking two sliders on top of each other using the jQuery Mobile framework. In addition, I have implemented JavaScript to ensure that the left thumb does not exceed the right one. However, I have encountered ...

What is the best way to incorporate tinymce into webpack?

I am facing an issue with integrating tinymce with webpack. It assigns a property called tinymce to window, so one solution is to use the following syntax to require() it (as explained in the bottom of the EXPORTING section of the webpack documentation): ...

Why is my jQuery $.ajax success function not providing any results?

When checking the Network tab in Chrome, I noticed that the correct data (action, username, password) is being sent, but the message is not returning to $('#return_login'). Can anyone spot what might be wrong with my code? Below is the jQuery co ...

Unable to reset HighCharts Speedometer value to zero

I am currently experimenting with implementing the highcharts speedometer. The goal is to simulate a connection meter - when there is a good connection, the meter should display values between 30 and 100. If the connection result from an Ajax call is any ...

Unknown error occurred in Eventstore: Unable to identify the BadRequest issue

I'm encountering an error while using Eventstore, specifically: Could not recognize BadRequest; The error message is originating from: game process tick failed UnknownError: Could not recognize BadRequest at unpackToCommandError (\node_modul ...

How to Delete Decimal Points from a String Using Regular Expressions

I am searching for a way in JavaScript, such as REGEX, to eliminate decimal numbers from a string. Here is an example input: Mazda 6 2.0 Skyactiv-G Wagon 2018 - Startstop.sk - TEST Mercedes C63 AMG Sound REVVING 6,3l V8 And here is the expected output: M ...

Is there a way to keep the modal window open in React without it automatically closing?

I have been working on a modal window implementation in React. I defined a state variable called modalbool to control the visibility of the modal window. The modal window is displayed only when modalbool is set to true. Additionally, I created a parent com ...

Order objects in a JavaScript array

Recently, I came across a list of student data fetched from an API: const Studentdata = { "StudentName1": { "active": true, "gender": "male" }, "StudentName2": { "active": false, "gender": "male" }, "S ...

Connecting Next.js to a Database: A Step-by-Step Guide

I am interested in developing an application similar to Samsung Health that will involve heavy querying on a database. I am unsure whether it would be more beneficial to create a custom server using Node.js (with Express.js) instead of using the integrate ...

Guide to updating the options of a UI select dynamically using AngularJS

I am working on implementing a dynamic feature for my UI Selects. Specifically, I have multiple UI Selects and I want to load choices into the second one based on the selection made in the first one. Despite my extensive search, I have not been able to fin ...

To view the contents of the dropdown list on an iPhone, simply tap the arrow key next to the mobile dropdown list and the contents will be

I am attempting to trigger the dropdown list contents to open/show by tapping on the arrow keys near AutoFill on the iPhone mobile keyboard. This action should mimic clicking on the dropdown itself. Currently, I am working on implementing this feature for ...

Alter the reply prior to being dispatched to the customer

Node 16.14.2, Express 4.18.1 There have been several instances where individuals have altered res.send in order to execute actions before the response is sent to the client. app.use(function (req, res, next) { originalSend = res.send; res.send = f ...

Using AXIOS and VueJs to visually represent data with a two-dimensional array

I'm new to vuejs and I want to display my data response in a two-dimensional table. This is the data I have: [{ "origine": "", "nb": 15 }, { "origine": "?", "nb": 18 }, { "origine": "?L", "nb": 1 }, { "origine": "G", "nb": 298 }, { ...

Employing a script that is generated from the jQuery AJAX response

How does using script as the response for a jQuery/AJAX request benefit us? Is it possible to directly run a function from that response script after sending the AJAX request? Any simple examples would be much appreciated. ...

Merge functions that execute identical operations

I find myself in a situation where I have two functions that do very similar tasks, but on different elements. Initially, I only anticipated having these two functions, but now I realize I'll need to add more. This will result in a lot of repetition. ...

Using Typescript to replicate Object.defineProperties

Is there a way to emulate Object.defineProperties from JavaScript in Typescript? I am interested in achieving something similar using the syntax of Typescript: Object.defineProperties(someObject.prototype, { property: {get: function() { return v ...

The configuration for CKEditor5's placeholder feature seems to be malfunctioning

I am currently experimenting with a customized version of CKEditor 5 known as BalloonBlockEditor. Below is the custom build output that I have created: /** * @license Copyright (c) 2014-2023, CKSource Holding sp. z o.o. All rights reserved. * For licens ...

Ways to customize easypiechart appearance with CSS styling

I am looking to create a circular counter similar to the one shown below. I have tried using easy-pie-chart but I am unsure how to style the circles to look like the example provided. Is there a way to achieve this through CSS? Any recommended resources o ...

Ways to switch out error message for an error landing page

I am currently displaying error text when something goes wrong, but I would like to enhance the user experience by redirecting to a well-designed error page instead. Can anyone provide guidance on how to achieve this? Below is my existing code for display ...

Navigatin// Accessing Chrome Windows Opened by External Applications Using Selenium and VBA

I am currently working with Excel VBA Selenium on a Windows 10 platform. My goal is to access a Chrome window that has been opened by another application, but I only know the initial part of its name as the rest is constantly changing. This specific window ...