html 的这一部分中使用的正确元素是什么?

What is the correct element to use from this section of html?

我正在使用 selenium 自动转到各种网页并下载 XML 文件。 这一直工作正常,但突然停止工作,这是由于硒无法找到该元素。 我试过将选择器小工具与 CSS & Xpath 一起使用。我试过直接从检查面板复制,我试过在 selenium 上使用等待函数,直到元素完全显示,但我没有运气。

这是下载按钮所在的html

<div class="video-playlist-xml" data-reactid=".2.0"><a href="#" data-reactid=".2.0.0"><i class="icon-download-xml-green" data-toggle="tooltip" data-placement="top" title="" data-original-title="Download xml file of the match" data-reactid=".2.0.0.0"></i></a></div>

下载按钮就在视频上方,比分线上方。

for x in range(number_of_clicks):
   driver = webdriver.Chrome(executable_path=r'C:\Users\James\OneDrive\Desktop\webdriver\chromedriver.exe',options = options)
   driver.get('https://football.instatscout.com/teams/978/matches')
   time.sleep(10)
   print("Page Title is : %s" %driver.title) 
   WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#team-table1 > div.table-scroll-inner > div.team-stats-wrapper.team-stats-wrapper_no-vertical-scroll > table > tbody > tr:nth-child(" +str(x+1) + ") > td:nth-child(1) > div > div.styled__MatchPlay-sc-10ytjn2-1.hkIvhi > i"))).click()
   WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#root > div > article > section.player-details > div > div.OutsideClickWrapper-sc-ktqo9u.cTxKts > div > a:nth-child(1) > span > span"))).click()
   WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.video-playlist-xml > a[href] > i.icon-download-xml-green[data-original-title='Download xml file of the match']"))).click()
   chks = driver.find_elements_by_css_selector("#players > div.control-block > div.control-block__container.control-block__container--large > button")
   for chk in chks:
       chk.click()
       time.sleep(15)
       driver.quit()

错误回溯

---------------------------------------------------------------------------
TimeoutException                          Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_26436/2420202332.py in <module>
      6    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#team-table1 > div.table-scroll-inner > div.team-stats-wrapper.team-stats-wrapper_no-vertical-scroll > table > tbody > tr:nth-child(" +str(x+1) + ") > td:nth-child(1) > div > div.styled__MatchPlay-sc-10ytjn2-1.hkIvhi > i"))).click()
      7    WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#root > div > article > section.player-details > div > div.OutsideClickWrapper-sc-ktqo9u.cTxKts > div > a:nth-child(1) > span > span"))).click()
----> 8    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.video-playlist-xml > a[href] > i.icon-download-xml-green[data-original-title='Download xml file of the match']"))).click()
      9    #WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.XPATH,'//*[contains(concat( " ", @class, " " ), concat( " ", "video-playlist-xml", " " ))]//a | //*[contains(concat( " ", @class, " " ), concat( " ", "icon-download-xml-green", " " ))]'))).click()
     10    chks = driver.find_elements_by_css_selector("#players > div.control-block > div.control-block__container.control-block__container--large > button")

~\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\selenium\webdriver\support\wait.py in until(self, method, message)
     78             if time.time() > end_time:
     79                 break
---> 80         raise TimeoutException(message, screen, stacktrace)
     81 
     82     def until_not(self, method, message=''):

TimeoutException: Message: 

定位 可点击的 元素而不是 you need to induce WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div.video-playlist-xml > a[href] > i.icon-download-xml-green[data-original-title='Download xml file of the match']"))).click()
    
  • 使用XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='video-playlist-xml']/a[@href]/i[@class='icon-download-xml-green' and @data-original-title='Download xml file of the match']"))).click()
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    

愚蠢的是,我刚刚意识到单击其中一个元素会打开一个新的 window。问题解决了。谢谢大家。