Python 元素在单击时过时

Python 元素在单击时过时,python,python-3.x,selenium,web-scraping,requests,Python,Python 3.x,Selenium,Web Scraping,Requests,我每次点击都会打开一个新页面,并从该页面下载视频。它适用于第一个页面,但当在for循环中单击第二个链接(链接单击打开新选项卡)时,当单击下载按钮时,下载按钮将失效,但是如果我选择传递此异常,则在第一次迭代中,不,仅在后续迭代中 from selenium import webdriver import time from selenium.webdriver.chrome.options import Options from selenium.webdriver.common.by impor

我每次点击都会打开一个新页面,并从该页面下载视频。它适用于第一个页面,但当在
for
循环中单击第二个链接(链接单击打开新选项卡)时,当单击下载按钮时,下载按钮将失效,但是如果我选择
传递此异常,则在第一次迭代中,,仅在后续迭代中

from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import Select
import os
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver import ActionChains
import pyautogui

chrome_options = Options()

prefs = {"download.default_directory" : "C:\\Users\\user\\Desktop\\"}
chrome_options.add_extension('1.5_0.crx') #This was not the issue. So, not needed.
chrome_options.add_experimental_option("prefs",prefs)
driver = webdriver.Chrome('C:/chromedriver.exe', options=chrome_options)
driver.get('https://portal.volleymetrics.hudl.com/#/auth/login')
actionChains = ActionChains(driver)
driver.maximize_window()
user = driver.find_element_by_xpath('//*[@id="username"]')
user.send_keys('USER')
password = driver.find_element_by_xpath('//*[@id="password"]')
password.send_keys('PASS') 
driver.find_element_by_xpath('//*[@id="login-content"]/form/button').click()
WebDriverWait(driver, 15).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#left-menu-container > div.left-menu-button-container-top > left-menu-button:nth-child(2)'))).click()
driver.find_element_by_xpath('//*[@id="portal-matches-tabs"]/vm-tabs/div/div[2]').click()
time.sleep(9)
'''driver.find_element_by_xpath('//*[@id="portal-matches-advanced-filters-text"]').click()
time.sleep(5)
start = driver.find_element_by_xpath('//*[@id="portal-matches-advanced-filters-container"]/div/ng-transclude/div[1]/div[2]/div[1]/vm-input-box/div[2]/div/input')
start.send_keys('01102020')
end = driver.find_element_by_xpath('//*[@id="portal-matches-advanced-filters-container"]/div/ng-transclude/div[1]/div[2]/div[2]/vm-input-box/div[2]/div/input')
end.send_keys('11012021')
driver.find_element_by_xpath('//*[@id="portal-matches-advanced-filters-container"]/div/ng-transclude/div[3]/button[1]').click()'''
main_div = driver.find_elements_by_xpath('//div[@class="generic-table-two-row-group"]')
main_div = main_div[1]
links = main_div.find_elements_by_class_name('my-matches-table-row-container')
count = 0
while(len(links)!=count):
    links = main_div.find_elements_by_class_name('my-matches-table-row-container')
    link=links[count]
    count+=1
    link.click()
    parent_window = driver.current_window_handle
    all_windows = driver.window_handles
    child_window = [window for window in all_windows if window != parent_window][0]
    driver.switch_to.window(child_window)
    video = WebDriverWait(driver, 80).until(EC.visibility_of_element_located((By.ID, 'vm-match-video')))
    if not video:
        break
    else:
        div1 = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CLASS_NAME, 'vm-match-actions-directive')))
        divs2 = div1.find_elements_by_class_name('button-container')
        count2 = 0
        while(len(divs2)!=count2):
            divs2 = div1.find_elements_by_class_name('button-container')
            div=divs2[count2]
            count2 +=1
            if 'Video' and 'Download' in div.text:
                link = div.find_element_by_xpath('/html/body/div[2]/div[2]/div/div/div/div[3]/div[2]/div[1]/div/div[3]/div[3]/vm-match-actions/div/div/div[1]/div[2]/a')
                driver.execute_script("arguments[0].scrollIntoView();", link)
                actionChains.context_click(link).perform()
                pyautogui.typewrite(['down', 'down', 'down', 'down', 'enter'])
                time.sleep(5)
                pyautogui.hotkey('enter')
                driver.refresh()
                WebDriverWait(driver, 80).until(EC.visibility_of_element_located((By.ID, 'vm-match-video')))
                try:
                    driver.find_element_by_xpath('//*[@id="portal-match-controls-column"]/div[3]/vm-match-actions/div/div/div[1]/div[4]/span[2]').click()
                    driver.find_element_by_xpath('//button[contains(text(),"I Accept")]').click()
                    time.sleep(6)
                    break
                except:
                    break
            else:
                continue
    driver.close()
    driver.switch_to.window(parent_window)
此错误表明找到了该元素,这就是为什么它位于
if
循环中,但在单击时会过时的原因

  Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/Freelance/stack.py", line 59, in <module>
    actionChains.context_click(link).perform()
  File "C:\Users\user\PycharmProjects\Freelance\venv\lib\site-packages\selenium\webdriver\common\action_chains.py", line 80, in perform
    self.w3c_actions.perform()
  File "C:\Users\user\PycharmProjects\Freelance\venv\lib\site-packages\selenium\webdriver\common\actions\action_builder.py", line 76, in perform
    self.driver.execute(Command.W3C_ACTIONS, enc)
  File "C:\Users\user\PycharmProjects\Freelance\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\user\PycharmProjects\Freelance\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
  (Session info: chrome=87.0.4280.141)
回溯(最近一次呼叫最后一次):
文件“C:/Users/user/PycharmProjects/freeloper/stack.py”,第59行,在
actionChains.context\u单击(链接).perform()
文件“C:\Users\user\PycharmProjects\freeloper\venv\lib\site packages\selenium\webdriver\common\action\u chains.py”,执行中第80行
self.w3c_actions.perform()
文件“C:\Users\user\PycharmProjects\freeloper\venv\lib\site packages\selenium\webdriver\common\actions\action\u builder.py”,第76行,执行
self.driver.execute(Command.W3C_ACTIONS,enc)
文件“C:\Users\user\PycharmProjects\freeloper\venv\lib\site packages\selenium\webdriver\remote\webdriver.py”,第321行,在execute中
self.error\u handler.check\u响应(响应)
检查响应中的文件“C:\Users\user\PycharmProjects\freeloper\venv\lib\site packages\selenium\webdriver\remote\errorhandler.py”第242行
引发异常类(消息、屏幕、堆栈跟踪)
selenium.common.exceptions.StaleElementReferenceException:消息:stale元素引用:元素未附加到页面文档
(会话信息:chrome=87.0.4280.141)
使用API和API,您可以获取所有数据、视频URL和下载视频

下面的代码收集视频URL。要下载您也可以使用的视频,请在此处查找详细信息:

您将获得:

{
  "2021.01.14-01.59-186589.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.14-01.59-186589.mp4",
  "2021.01.13-09.49-178458.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.13-09.49-178458.mp4",
  "2021.01.11-15.11-178456.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.11-15.11-178456.mp4",
  "2021.01.08-09.44-184883.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.08-09.44-184883.mp4",
  "2020.11.20-19.43-179457.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.20-19.43-179457.mp4",
  "2020.11.11-01.26-178151.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.11-01.26-178151.mp4",
  "2020.11.11-01.45-178117.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.11-01.45-178117.mp4",
  "2020.11.10-10.48-177923.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.10-10.48-177923.mp4",
  "2020.11.06-18.50-176707.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.06-18.50-176707.mp4",
  "2020.12.28-13.54-177032.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.12.28-13.54-177032.mp4",
  "2020.10.29-23.46-175761.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.29-23.46-175761.mp4",
  "2020.10.28-19.01-175566.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.28-19.01-175566.mp4",
  "2020.10.23-19.27-174548.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.23-19.27-174548.mp4",
  "2020.10.16-18.39-172318.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.16-18.39-172318.mp4",
  "2020.10.15-19.04-172317.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.15-19.04-172317.mp4",
  "2020.10.15-12.43-172783.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.15-12.43-172783.mp4",
  "2020.10.14-19.02-172316.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.14-19.02-172316.mp4",
  "2020.10.14-11.04-172417.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.14-11.04-172417.mp4"
}
使用API和API,您可以获取所有数据、视频URL和下载视频

下面的代码收集视频URL。要下载您也可以使用的视频,请在此处查找详细信息:

您将获得:

{
  "2021.01.14-01.59-186589.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.14-01.59-186589.mp4",
  "2021.01.13-09.49-178458.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.13-09.49-178458.mp4",
  "2021.01.11-15.11-178456.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.11-15.11-178456.mp4",
  "2021.01.08-09.44-184883.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2021.01.08-09.44-184883.mp4",
  "2020.11.20-19.43-179457.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.20-19.43-179457.mp4",
  "2020.11.11-01.26-178151.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.11-01.26-178151.mp4",
  "2020.11.11-01.45-178117.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.11-01.45-178117.mp4",
  "2020.11.10-10.48-177923.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.10-10.48-177923.mp4",
  "2020.11.06-18.50-176707.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.11.06-18.50-176707.mp4",
  "2020.12.28-13.54-177032.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.12.28-13.54-177032.mp4",
  "2020.10.29-23.46-175761.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.29-23.46-175761.mp4",
  "2020.10.28-19.01-175566.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.28-19.01-175566.mp4",
  "2020.10.23-19.27-174548.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.23-19.27-174548.mp4",
  "2020.10.16-18.39-172318.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.16-18.39-172318.mp4",
  "2020.10.15-19.04-172317.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.15-19.04-172317.mp4",
  "2020.10.15-12.43-172783.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.15-12.43-172783.mp4",
  "2020.10.14-19.02-172316.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.14-19.02-172316.mp4",
  "2020.10.14-11.04-172417.mp4": "http://vm-transcoded-videos.s3.amazonaws.com/2020.10.14-11.04-172417.mp4"
}

这是
while
循环,而不是
for
循环。尝试为每次迭代创建一个新的驱动程序?我希望你编辑了你的pw,
12345678
不是你真正的密码这是
while
循环,不是
for
循环。尝试为每个迭代创建一个新的驱动程序?我希望你编辑了你的pw,
12345678
不是你真正的密码返回
没有视频url
对于每个请求,你能重新检查吗?工作没有问题。更新代码以确保没有丢失任何内容。请再试一次,不要在对此的响应中看到视频url。get(f'{match[“id”]}')确保您有准确的代码。它应该是:
r=session.get(f'https://api.volleymetrics.hudl.com/analysis/matches/{match[“id”]})video_url=r.json()['encodedVideoUrl']
。再次感谢!为每个请求返回
无视频url
,是否可以重新检查此项?无问题工作。更新代码以确保没有丢失任何内容。请再试一次,不要在对此的响应中看到视频url。get(f'{match[“id”]}')确保您有准确的代码。它应该是:
r=session.get(f'https://api.volleymetrics.hudl.com/analysis/matches/{match[“id”]})video_url=r.json()['encodedVideoUrl']
。再次感谢!