Tips for running Scrapy and Selenium on a webpage that utilizes angular JavaScript to serve data

I have been working on a web scraper that follows this process:

Visit site A -> click on the buy now button -> redirected to Amazon -> scrape data -> return to site A

The issue I am facing is that the site is built using AngularJS, and I am having trouble clicking the button with selenium.click()

To scrape the JavaScript page, I am using Selenium and Scrapy. The page has infinite scrolling, so I need a solution that works for such pages.

Below is the HTML snippet of the element in question:

<a class="external" href="http://www.amazon.com/dp/B01DBR53FU/?tag=097-20&amp;ascsubtag=v7_3_3_3m7_2nhz_0_x01_-srt5-" target="_blank" analytics-on="click" analytics-event="button" analytics-category="outbound" analytics-label="non-lethal-salt-firing-self-defense-gun" ng-click="click(post)" rel="nofollow">Check it out </a>

The syntax I attempted to use was:

 sel.xpath('//*[@class="button"]').click()

This resulted in an error message:

'SelectorList' object has no attribute 'click'

Answer №1

'SelectorList' object is unable to perform the 'click' action

This error indicates that the click method cannot be used on the element selected by your xpath.

To resolve this issue in Python, you can try the following:

    element_to_click = self.driver.find_element_by_css_selector('a.external')
 driver.execute_script("arguments[0].scrollIntoView(true);",element_to_click)
    element_to_click.click()

Alternatively, you can use the xpath method like this:

element_to_click = 
    self.driver.find_element_by_xpath("//a[contains(@href,'http://www.amazon.com/dp/B01DBR53FU/?tag=097-20&amp;ascsubtag=v7_3_3_3m7_2nhz_0_x01_-srt5-')]")
#assuming that the href doesn't change.

driver.execute_script("arguments[0].scrollIntoView(true);",element_to_click)   

# scroll element into view   

element_to_click.click()

Another option is to use linkText as suggested in another answer.

Edit 3: An alternative solution is sending a JS click to this locator

driver.execute_script("arguments[0].click();",element_to_click) 

Edit 4 : Pay close attention to this edit. Clicking on any gift item on this page is straightforward

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chrome_path  = 'your chromedriver path here'

driver = webdriver.Chrome(chrome_path)

driver.get("http://thisiswhyimbroke.com/gifts/gifts-for-men/")
time.sleep(5)

#clicks on the first gift article
first_article = driver.find_element_by_css_selector('#page-gifts > article:nth-child(1) > div.button > a')
first_article.click()
time.sleep(5)

All the gift articles mentioned are generated by a repeater since it's an Angular web page. They are contained within an article tag.

If you need to click on a different article button, simply adjust the article:nth-child to target the desired child and the code will function accordingly.

Prior to beginning automation, familiarize yourself with creating and utilizing locators to ensure effective automation implementation.

Answer №2

To target elements, you have the option of utilizing CSS Selector like

".external"

Alternatively, you can also target elements by Link Text such as

"Check it out" 

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

What strategies can be used to effectively structure CSS and JavaScript in a project for optimal organization?

In my NetBeans project, I currently have a large web project with CSS files included in the header. Some CSS codes are needed on all pages, while others are only necessary for specific pages. I am looking to optimize high-traffic pages by removing any ...

Error: The jarfile node_modules/protractor/selenium/selenium-server-standalone-2.45.0.jar is not accessible by Grunt

Recently started using Grunt and encountered a problem that I could use some help with. Whenever I try to run the grunt command, I get the following error message: Error when running jasmine_node task Error: Unable to access jarfile node_modules/p ...

Style binding for background image can utilize computed properties or data for dynamic rendering

In my code, I am trying to pass an object that contains a string path for its background image. I have experimented with using data and computed properties, but so far I haven't had any luck getting them to work within the :style binding. However, if ...

Can you explain the distinction between Array() and [] in Javascript, and when would it be preferable to use one over the other?

Similar Question: Understanding the difference between "new Array()" and "[]" in JavaScript array declaration When working with JavaScript, you have the option to create a new array using: var arr = new Array(); or simply using: var arr2 = []; Wha ...

Issues with Ajax Requests

The pending requests from the Post need to be completed. I was hoping to record the JSON body of the request that comes in. Everything works fine when using Postman, but for some reason the AJAX requests are not functioning properly. Code snippet for Node ...

How to prevent npm from being accessed through the command prompt

I recently began working on a ReactJs project. However, I am facing an issue where after starting npm in Command Prompt, I am unable to enter any text. Should I close the cmd window or is there a way to stop npm? You can now access your project at the fol ...

Explication of syntax not functioning

Following the instructions provided here but encountering issues, any assistance? <script type="text/javascript" src="sh/src/shCore.js"></script> <script type="text/javascript" src="sh/scripts/shBrushJScript.js"></script> <lin ...

Interfacing shared memory between a C++ and JavaScript program

Is it feasible to have shared memory that both a C++ program and a JavaScript program can access simultaneously? The goal is for the C++ program to write to memory while the JS program reads from the same location. ...

Locating the xpath of an element directly beneath the opening <body> tag

I'm having trouble finding the xpath for an element that is not associated with any html tags. Please see the attached image for reference. I tried using driver.findElement(By.xpath("/html/body/"));, but it's not working. I need to locate the tex ...

Executing Javascript on Selenium RC with PHP is a crucial skill to have in your toolkit

Is there a way to execute Javascript from Selenium RC? I have been trying different methods with no success so far. I attempted the following approach: I created a custom function in user-extensions.js: function sayhello() { document.write('hel ...

Embedding JSON data in a GSP page

My goal is to transfer JSON data to a GSP page and present it in a table format. The expected JSON structure: { "data": [ [ "Tiger Nixon", "System Architect", "Edinburgh" ] ]} I attempted to achieve this with the following co ...

Utilizing d3.js to implement a scatterplot with zoom functionality that focuses solely on zooming the axis without affecting

Having trouble creating a scatterplot with zoom functionality where only the axis is getting zoomed, not the data itself. Can anyone provide some assistance or insight on what might be wrong? If you're interested in checking out the project, here&apo ...

Error occurs when an arrow function is called before its function definition

console.log(addB(10, 15)); function addB(a, b) { return a + b; } console.log(addC(10, 15)); const addC = (a, b) => { return a + b; }; I attempted to convert a function into an arrow function and encountered the error "Cannot access 'addC&ap ...

The custom filter in AngularJS fails to activate when a click event occurs

I am trying to customize the filtering of my table data based on selected conditions from a dropdown menu. I have created an AngularJS custom filter and passed all the necessary parameters. The desired functionality is that if no conditions are selected, ...

Refreshing a particular <div> on a webpage while making an AJAX request

I've encountered an issue that has left me stuck. The problem is that I need to refresh a specific div on my page that contains PHP script. Below is the JavaScript function causing trouble: function select_dayoff() { jQuery('#loader').c ...

Deleting an HTML column that has a dynamic header name <th> can be achieved by following these steps

I have a script that can add a new column to an HTML table. When the user clicks on add group, the header will change to Group1, Group2, and so on. I am currently adding a function for delete group that can delete all the added columns. The issue now is th ...

Successive vows

I'm trying to retrieve responses from four promises, but I currently have to call each function in sequence one after the other. In my code, you can see that I trigger the next function within the promise callback of the previously called function. H ...

Unordered calling of functions in JavaScript - is it possible?

I'm currently working on a project that involves extracting data from an SQL database and converting the output of a query (which is a number) into a corresponding color, which is then passed to a JavaScript variable. Essentially, I am using ajax to ...

Update the CSS for InputLabel

I have a drop-down list that I want to customize. The issue is illustrated below: I'm looking to center the text "choose format" within the field and adjust the font size. return ( <FormControl sx={{ m: 1, minWidth: 150 }} size="sm ...

What is the proper way to insert a line break within a string in HTML code?

Trying to simplify my code, I've turned to using Nunjucks to pass a group of strings to a function that will then display them. This is what it looks like: {% macro DisplayStrings(strings) %} {% for item in strings %} <div>{{ item.strin ...