微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

下载文件 selenium python 后的陈旧元素

如何解决下载文件 selenium python 后的陈旧元素

在点击链接后尝试下载视频。它适用于第一页,但是当第二个链接打开一个页面时,它说该元素已失效同时它正在右键单击它。

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.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_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('merin_sinha24')
password = driver.find_element_by_xpath('//*[@id="password"]')
password.send_keys('password') #That is not the password
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,50).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_tag_name('a')
                actionChains.context_click(link).perform()
                pyautogui.typewrite(['down','down','enter'])
                time.sleep(5)
                pyautogui.hotkey('enter')
                driver.refresh()
                webdriverwait(driver,30).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)

解决方法

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)
    time.sleep(13)
    error = driver.find_element_by_class_name('video-message')
    if error.text == 'No video to display.':
        break
    else:
        div1 = WebDriverWait(driver,20).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=divs[count2] 
            count2+ =1
            if 'Video' and 'Download' in div.text:
                link = div.find_element_by_tag_name('a')
                actionChains.context_click(link)
                actionChains.perform()
                pyautogui.typewrite(['down','down','enter'])
                time.sleep(5)
                pyautogui.hotkey('enter')
                driver.refresh()
                time.sleep(15)
                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(4)
                    break
                except:
                    break
            else:
                continue
    driver.close()
    driver.switch_to.window(parent_window)

             

你应该总是再次找到 divs ,因为你点击“链接”,这会修改 DOM,当 DOM(页面 html)被修改时,你会再次找到所有元素,因为之前的引用丢失了,这就是你得到的原因陈旧元素

您可以使用 driver.back() 向后导航

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。