2

I want to download the files of all teams for season 2015-2016 to 2018-2019. However, I am trying to loop through Xpaths that are identical except one number in a bracket to select the different teams and years; the last bracket where I replaced the number with %b and %i. Here is my code:

from selenium import webdriver
import csv
from selenium.webdriver.support.ui import Select
from datetime import date, timedelta
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC
import time
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import TimeoutException 

chromedriver =("C:/Users/Michel/Desktop/python/package/chromedriver_win32/chromedriver.exe")
driver = webdriver.Chrome(chromedriver)
driver.get("https://evolving-hockey.com/")

#Click Games and then game logs
Gamestab= driver.find_element_by_xpath("/html/body/nav/div/ul/li[6]/a")
Gamestab.click()
Gameslog= driver.find_element_by_xpath("/html/body/nav/div/ul/li[6]/ul/li[3]/a")
Gameslog.click()


# Click Teams tab 
Teamstab= driver.find_element_by_xpath("//*[@id='tab-3278-3']/div/ul/li[3]/a")
Teamstab.click()


# Loop all teams and all seasons
## TEAM

for b in range(1,33):

    Team= driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[1]/div/div/div")
    Team.click()
    Teamname= driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[1]/div/div/div/div[2]/div/div[%b]" %(b))
    Teamname.click()


# ## Season- 20152016to20182019


    for i in range(1,5):
        Season=driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[2]/div/div/button")
        Season.click()
        Season1819=driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[2]/div/div/div/ul/li[%s]" %(i))
        Season1819.click()

I think it should work by using % and assigning a variable that is, in fact, the iterative element in the for loop, just like I tried, but it does not work.

4
  • I would recommend get all the teamNames using find_elements_by_xpath rather find_elemnt_by_xpath and then click on each of them. Let me know if you need a sample code. Commented Mar 8, 2019 at 6:38
  • I would like a solution, please. I edited my code so you can access the website and inspect it. I feel it should work with the logic I am using...I did something similar in the past, but the changing element was a string instead of an integer. I am wondering if I should proceed in another way, because of that. Commented Mar 8, 2019 at 7:08
  • What is the error that you are getting ? Commented Mar 8, 2019 at 7:14
  • "/div[%b]" %(b))" should this be "/div[%d]" %(b))" Commented Mar 8, 2019 at 7:27

1 Answer 1

1

If you want to use your existing code then please correct the below line. Just changed the [%b] to [%d] at the end of the xpath string.

Old code:

Teamname= driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[1]/div/div/div/div[2]/div/div[%b]" %(b))

Updated Code:

Teamname= driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[1]/div/div/div/div[2]/div/div[%d]" %(b))

Here is the refracted code. I haven't got a chance to test this.

    #click on Games
    driver.find_element_by_css("ul.nav.navbar-nav a[data-value='Games']").click()
    #click on Game Logs
    driver.find_element_by_css_selector("ul.dropdown-menu a[data-value='Game Logs']").click()
    #switch to Teams tab
    driver.find_element_by_css_selector("ul.nav.nav-tabs a[data-value='Teams']").click()
    #click the teams listbox
    teamNames = driver.find_element_by_xpath("//div[@class='tab-pane active' and @data-value='Teams']//label[.='Team:']//parent::div//div[@class='selectize-dropdown-content']").click()
    #get the list of team names
    teams = driver.find_elements_by_xpath("//div[@class='tab-pane active' and @data-value='Teams']//label[.='Team:']//parent::div//div[@class='selectize-dropdown-content']//div[@class='option']")

    # get the list of seasons
    seasons = driver.find_elements_by_xpath("//div[@class='tab-pane active' and @data-value='Teams']//select[@id='game_logs_teams_season']/option")
    # iterate through each team
    for team in teams:
        team.click()
        # iterate through each season
        for season in seasons:
            seanson.click()
Sign up to request clarification or add additional context in comments.

4 Comments

what is the error? and did you tried replacing "/div[%b]" %(b))" with "/div[%d]" %(b)) in your old logic?
It is working with : "/div[%b]" %(b))" with "/div[%d]" %(b)).
I have another question. There is a part that seems to change everyday; [@id='tab-3959-3'] is in every find_by_xpath, but the number changed from yesterday ? What could I do ?
Ideally you don't need that "id" to identify the elements. Check the css and xpaths that I used in my sample code provided. I vetted all those css and xpaths in the UAT page that you provided.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.