Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过XPATH和PYTHON循环_Python_Loops_Selenium - Fatal编程技术网

通过XPATH和PYTHON循环

通过XPATH和PYTHON循环,python,loops,selenium,Python,Loops,Selenium,我想下载2015-2016赛季至2018-2019赛季所有球队的档案。然而,我试图通过循环相同的XPath,除了括号中的一个数字之外,来选择不同的团队和年份;我用%b和%I替换数字的最后一个括号。 这是我的密码: from selenium import webdriver import csv from selenium.webdriver.support.ui import Select from datetime import date, timedelta from selenium.w

我想下载2015-2016赛季至2018-2019赛季所有球队的档案。然而,我试图通过循环相同的XPath,除了括号中的一个数字之外,来选择不同的团队和年份;我用%b和%I替换数字的最后一个括号。 这是我的密码:

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()

我认为它应该通过使用%并分配一个变量来工作,事实上,这个变量是for循环中的迭代元素,就像我尝试过的那样,但它不起作用

如果要使用现有代码,请更正下面的行。刚刚将xpath字符串末尾的[%b]更改为[%d]

旧代码:

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= driver.find_element_by_xpath("//*[@id='tab-3959-3']/div/div[1]/div[1]/div/div/div/div[2]/div/div[%d]" %(b))
这是折射代码。我没有机会测试这个

    #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()

我建议使用find_elements_by_xpath获取所有团队名称,而不是使用find_elemnt_by_xpath,然后单击每个团队名称。如果你需要一个示例代码,请告诉我。我想要一个解决方案。我编辑了我的代码,所以你可以访问网站并检查它。我觉得它应该与我正在使用的逻辑一起工作……我在过去做过类似的事情,但是改变的元素是字符串而不是整数。我想知道是否应该以另一种方式继续,因为这样。您得到的错误是什么?“/div[%b]”%(b))“这应该是“/div[%d]”%(b))”错误是什么?您是否尝试在旧逻辑中将“/div[%b]”%(b))”替换为“/div[%d]”%(b))?它正在使用“/div[%b]”%(b))”替换为“/div[%d]”%(b))。我还有一个问题。有一部分似乎每天都在变化;[@id='tab-3959-3']在xpath的每个find_中都有,但是数字从昨天起发生了变化?我能做什么?理想情况下,你不需要那个“id”来识别元素。检查我在提供的示例代码中使用的css和XPath。我检查了您提供的UAT页面中的所有css和XPath。