Skip to content
Advertisement

Problem with looping over XPaths selenium

I’m trying to collect news articles off yahoo finance using selenium. I got it to work with 1 article, but when I try looping over the different articles it doesn’t make the click. The reason I have the second ‘except’ ‘continue’ is because there are ads in between the articles which I don’t want to click. The structure of the XPath of the articles are either somthingdiv[1] or somethingdiv[2] and the ‘li[]’ part differs for every article (+1 for every article). Does someone have any idea what I’m doing wrong? Or does someone have a better way to do this?

Here is my current code:

JavaScript

Advertisement

Answer

I suggest you to look do it using BeatuifulSoup.

You just need to scape the webpage. Selenium is useful when you need a real browser to click, input values, navigate to other pages ect…

With BeatifulSoup it will be easyer and faster.

You can do something like this:

JavaScript

This is just an example. You can scrape whatever you want like link to the articles etc…

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement