seleniumbeautifulsoupsplinter

Splinter - Element is not clickable because another element <p> obscures it


I am trying to get some thumbnail pictures from a website, from src, as well as click on a link, so I can later get the big picture.

For that I'm using Splinter with BeautifulSoup.

This is the htmlfor the first element I need to get:

enter image description here

In order to do that I have the following code:

executable_path = {"executable_path": "/path/to/geckodriver"}
browser = Browser("firefox", **executable_path, headless=False

def get_player_images():

    url = f'https://www.premierleague.com/players'

    # Initiate a splinter instance of the URL
    browser.visit(url)

    browser.find_by_tag('div[class="table playerIndex"]')
    soup = BeautifulSoup(browser.html, 'html.parser')
    for el in soup:
        td =  el.findAll('td')
        for each_td in td:
            link = each_td.find('a', href=True)
            if link:
                print (link['href'])
            image = each_td.find('img')
            if image:
                print(image['src'])
# run
get_player_images()

But I'm running into 2 issues, after browser opens:

I'm accessing only first two actual src for players. After that, photos are missing, which I don't get why.

/players/19970/Max-Aarons/overview
https://resources.premierleague.com/premierleague/photos/players/40x40/p232980.png
/players/13279/Abdul-Rahman-Baba/overview
https://resources.premierleague.com/premierleague/photos/players/40x40/p118335.png
/players/13286/Tammy-Abraham/overview
//platform-static-files.s3.amazonaws.com/premierleague/photos/players/40x40/Photo-Missing.png
/players/3512/Adam-Smith/overview
//platform-static-files.s3.amazonaws.com/premierleague/photos/players/40x40/Photo-Missing.png
/players/10905/Che-Adams/overview
....

Also, if I try to click on href link, with:

if link:
   browser.click_link_by_partial_href(link['href'])

I get the error:

selenium.common.exceptions.ElementClickInterceptedException: Message: Element <a class="playerName" href="/players/19970/Max-Aarons/overview"> is not clickable at point (244,600) because another element <p> obscures it

What am I doing wrong? I'm running into a lot of troubles with selenium.


Solution

  • The player data is loaded dynamically via Javascript. You can use requests module to obtain the info.

    For example:

    import re
    import json
    import requests
    from bs4 import BeautifulSoup
    
    
    url = 'https://footballapi.pulselive.com/football/players?pageSize=30&compSeasons=274&altIds=true&page={page}&type=player&id=-1&compSeasonId=274'
    img_url = 'https://resources.premierleague.com/premierleague/photos/players/250x250/{player_id}.png'
    headers = {'Origin': 'https://www.premierleague.com'}
    
    for page in range(1, 10):       # <--- increase this to desired number of pages
        data = requests.get(url.format(page=page), headers=headers).json()
    
        # uncoment this to print all data:
        # print(json.dumps(data, indent=4))
    
        for player in data['content']:
            print('{:<50} {}'.format(player['name']['display'], img_url.format(player_id=player['altIds']['opta'])))
    

    Prints:

    Ethan Ampadu                                       https://resources.premierleague.com/premierleague/photos/players/250x250/p199598.png
    Joseph Anang                                       https://resources.premierleague.com/premierleague/photos/players/250x250/p447879.png
    Florin Andone                                      https://resources.premierleague.com/premierleague/photos/players/250x250/p93284.png
    André Gomes                                        https://resources.premierleague.com/premierleague/photos/players/250x250/p120250.png
    Andreas Pereira                                    https://resources.premierleague.com/premierleague/photos/players/250x250/p156689.png
    Angeliño                                           https://resources.premierleague.com/premierleague/photos/players/250x250/p145235.png
    Faustino Anjorin                                   https://resources.premierleague.com/premierleague/photos/players/250x250/p223332.png
    Michail Antonio                                    https://resources.premierleague.com/premierleague/photos/players/250x250/p57531.png
    Cameron Archer                                     https://resources.premierleague.com/premierleague/photos/players/250x250/p433979.png
    Archie Davies                                      https://resources.premierleague.com/premierleague/photos/players/250x250/p215061.png
    Stuart Armstrong                                   https://resources.premierleague.com/premierleague/photos/players/250x250/p91047.png
    Marko Arnautovic                                   https://resources.premierleague.com/premierleague/photos/players/250x250/p41464.png
    Kepa Arrizabalaga                                  https://resources.premierleague.com/premierleague/photos/players/250x250/p109745.png
    Harry Arter                                        https://resources.premierleague.com/premierleague/photos/players/250x250/p48615.png
    Daniel Arzani                                      https://resources.premierleague.com/premierleague/photos/players/250x250/p200797.png
    
    ... and so on.
    

    Note: to get smaller thumbnails, change 250x250 in the image URLs to 40x40