8

I am attempting to use Python and Selenium to re-order the players in my fantasy football team's Pre-Draft Strategy page. Logging in and getting to the page is fine, but attempting to swap the players around is causing some issues.

So far I have used the following code but without success:

import nfl_useful_functions as nfl
from selenium.webdriver import ActionChains
import time

## Login to ESPN
driver = nfl.login2espn()
## Go to draft strategy page
driver.get("http://fantasy.espn.com/football/editdraftstrategy?leagueId=123456")
## Wait 10 seconds
time.sleep(10)
## Choose the player to be dragged (player 1)
draggable = driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[5]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/section[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/div[1]/div[2]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[1]")
## Choose the player for player 1 to be dragged onto (and swapped with)
droppable = driver.find_element_by_xpath("/html[1]/body[1]/div[1]/div[1]/div[1]/div[1]/div[5]/div[2]/div[1]/div[1]/div[2]/div[2]/div[1]/div[2]/section[1]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/div[1]/div[2]/table[1]/tbody[1]/tr[1]/td[1]/div[1]/table[1]/tbody[1]/tr[2]")
## Drag and drop
#Attempt 1
ActionChains(driver).drag_and_drop(draggable, droppable).perform()
#Attempt 2
ActionChains(driver).drag_and_drop_by_offset(draggable, 0, 40).perform()
#Attempt 3
ActionChains(driver).click_and_hold(draggable)\
                    .move_to_element(droppable)\
                    .release(draggable)\
                    .perform()
#Attempt 4
ActionChains(driver).click_and_hold(draggable)\
                    .move_to_element_with_offset(draggable,0,40)\
                    .release(draggable)\
                    .perform()
## Wait 5 seconds
time.sleep(5)
## Close driver
driver.close()

I have tried each of the different attempts (individually) but the only (small) positive is that the player I'm trying to move does appear to be selected:

enter image description here

But nothing actually comes of this. The script runs successfully (by that I mean without error) each time. The player I'm trying to drag goes grey, as above, for a two or three seconds, then seems to give up and the script ends. No exceptions or anything.

Is there anything else I can try? Ideally it would still be in Python other than that I'm very open to any suggestions! If it's useful, according to Wappalyzer, the details of the page in question are: enter image description here

EDIT

This is how the page looks when I swap players over manually. This is how the page looks when I run the script above (Attempt 1).

EDIT 2

I have created a throwaway account on ESPN in order to make recreation of the problem easier. Login to ESPN Fantasy using od44@live.co.uk & Pa55word123 and then go to the Pre-Draft Strategy Page.

OD1995
  • 1,647
  • 4
  • 22
  • 52
  • Hi, please give more info. Is all the code working fine (except that it doesn't do what you want)? no errors? at which part it breaks? What is the output? – simkusr Aug 07 '19 at 04:49
  • @simkus I have updated the question, basically the code produces no errors but also not the expected outcome – OD1995 Aug 07 '19 at 15:01
  • Could you please clarify if the 'going grey' behaviour is expected? Could you maybe use [ScreenToGif](https://www.screentogif.com/) to make a short gif of what's expected, and what the driver window is doing? – Geza Kerecsenyi Aug 10 '19 at 13:27
  • @GezaKerecsenyi I have added what is expected/what happens in to the question, let me know if there's anything else I can add that would be helpful – OD1995 Aug 11 '19 at 02:00
  • @OD1955 - thanks, these GIFs are very helpful! I'll see if I can come up with a solution. – Geza Kerecsenyi Aug 11 '19 at 06:27
  • Attempt 3 should be working in theory. However, a lot of people on the internet seem to take issue with this functionality, so I'm not surprised it's not working (they all describe the same 'symptoms' as you - clicking but not moving, failing silently). One suggestion I've found is to implement drag and drop in JavaScript instead - is this a possibility for you? If so, I can help you with the code for it. – Geza Kerecsenyi Aug 11 '19 at 06:40
  • @OD1995 Can you help me with the steps to reach to the intended page? Do we need any credential? Do you have a demo set of credentials? – undetected Selenium Aug 12 '19 at 19:59
  • @DebanjanB I have added in an account you can use to recreate the problem – OD1995 Aug 13 '19 at 06:36

1 Answers1

0

Ok, so I tried to recreate the problem and I noticed that it actually can change the position of the player but only if the mouse cursor is over another player. You can see it on the video here

I've tried to use headless selenium hoping that the problem won't happen again but headless selenium fails as well

So knowing that the problem (besides selenium) is the position of the cursor I used pyautogui to move cursor to position of the place where we want to drop the player:

from time import sleep

import pyautogui
from selenium import webdriver
from selenium.webdriver import ActionChains

driver = webdriver.Chrome()
# logged in manually

## Choose the player to be dragged (player 1)
draggable = driver.find_element_by_xpath("//div[@title='Rank' and text()=1]")
## Choose the player for player 1 to be dragged onto (and swapped with)
droppable = driver.find_element_by_xpath("//div[@title='Rank' and text()=2]")

pyautogui.moveTo(droppable.location['x'], droppable.location['y'])
sleep(0.1)
ActionChains(driver).drag_and_drop(draggable, droppable).perform()

This "hack" worked for me. You can see how it works on video here

Important Note!

Even though that this worked (remember to use selenium without toolbar so the Y position don't need to be adjusted). It's not the perfect solution because you need to move the cursor, in perfect world you could just use drag_and_drop method. But if you think that it will work for your personal project I think you can go for it

puchal
  • 1,883
  • 13
  • 25
  • Thanks for your answer! One question: how do I use selenium without toolbar? – OD1995 Aug 16 '19 at 02:02
  • 1
    @OD1995 You can add argument '--app=http://www.google.com' in ChromeOptions ``` chrome_options = ChromeOptions() ; chrome_options.add_argument('--app=http://www.google.com') ; driver = webdriver.Chrome(options=chrome_options) ``` – puchal Aug 16 '19 at 11:50