I am opening a new page with every click and downloading a video from that page. It works for the first page, but when the second link is clicked in the for
loop(link clicks open a new tab), the Download button goes stale when the Download button is clicked, but not on the first iteration, only on the subsequent ones, if I choose to pass
this exception.
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)
This error suggests the element was found that is why it is inside the if
loop, but goes stale on click?
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/Freelance/stack.py", line 59, in <module>
actionChains.context_click(link).perform()
File "C:UsersuserPycharmProjectsFreelancevenvlibsite-packagesseleniumwebdrivercommonaction_chains.py", line 80, in perform
self.w3c_actions.perform()
File "C:UsersuserPycharmProjectsFreelancevenvlibsite-packagesseleniumwebdrivercommonactionsaction_builder.py", line 76, in perform
self.driver.execute(Command.W3C_ACTIONS, enc)
File "C:UsersuserPycharmProjectsFreelancevenvlibsite-packagesseleniumwebdriver
emotewebdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "C:UsersuserPycharmProjectsFreelancevenvlibsite-packagesseleniumwebdriver
emoteerrorhandler.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)