在遍历URL时如何解决动态DOM问题



我想做以下事情:

  1. 打开URL
  2. 点击下载
  3. 打开具有新URL的新选项卡
  4. 关闭上一个选项卡
  5. 单击新URL上的下载(新选项卡(
  6. 重复

这是我的代码(底部错误(:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
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 datetime import datetime, timedelta

# Load Chrome driver and movement.uber.com/cities website
PATH = 'C:Program Files (x86)chromedriver.exe'
driver = webdriver.Chrome(PATH)
driver.get('https://movement.uber.com/explore/atlanta/travel-times/query?si1074&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]=2016-01-02&dt[dr][ed]=2016-01-02&cd=&sa;=&sdn=&lat.=33.7489&lng.=-84.4234622&z.=12&lang=en-US')

# Clicking on download (note that this download the dataset I need, but it shows the point I want to make)
download_button = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/div/div[3]/button')
download_button.click()

# Open new tab
driver.execute_script("window.open('https://movement.uber.com/explore/atlanta/travel-times/query?si1074&ti=&ag=taz&dt[tpb]=ALL_DAY&dt[wd;]=1,2,3,4,5,6,7&dt[dr][sd]=2016-01-02&dt[dr][ed]=2016-01-02&cd=&sa;=&sdn=&lat.=33.7489&lng.=-84.4234622&z.=12&lang=en-US')")

# Switch to the previous tab and close it (leaving us with the new above-opened tab)
tabs = driver.window_handles
if len(tabs) > 1:
driver.switch_to.window(tabs[0])
driver.close()
driver.switch_to.window(tabs[1])

# Click on download AGAIN, but in the newest window (this is where I have the problem)
download_button.click()

我得到的错误是:

StaleElementReferenceException            Traceback (most recent call last)
<ipython-input-88-1b0df7cfcd96> in <module>
----> 1 download_button.click()
c:usersi539797appdatalocalprogramspythonpython38-32libsite-packagesseleniumwebdriverremotewebelement.py in click(self)
78     def click(self):
79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
81 
82     def submit(self):
c:usersi539797appdatalocalprogramspythonpython38-32libsite-packagesseleniumwebdriverremotewebelement.py in _execute(self, command, params)
631             params = {}
632         params['id'] = self._id
--> 633         return self._parent.execute(command, params)
634 
635     def find_element(self, by=By.ID, value=None):
c:usersi539797appdatalocalprogramspythonpython38-32libsite-packagesseleniumwebdriverremotewebdriver.py in execute(self, driver_command, params)
319         response = self.command_executor.execute(driver_command, params)
320         if response:
--> 321             self.error_handler.check_response(response)
322             response['value'] = self._unwrap_value(
323                 response.get('value', None))
c:usersi539797appdatalocalprogramspythonpython38-32libsite-packagesseleniumwebdriverremoteerrorhandler.py in check_response(self, response)
240                 alert_text = value['alert'].get('text')
241             raise exception_class(message, screen, stacktrace, alert_text)
--> 242         raise exception_class(message, screen, stacktrace)
243 
244     def _value_or_default(self, obj, key, default):
StaleElementReferenceException: Message: stale element reference: element is not attached to the page document
(Session info: chrome=84.0.4147.105)

当我在新选项卡上时,如何绕过这个动态DOM问题并能够毫无问题地点击下载按钮?

我的最终目标是迭代不同日期的URL,并点击每个URL上的下载,这样我就可以分别下载每天的数据集。这就是为什么我需要访问不同的URL。

总是在调用.click之前再次初始化download_button

# Click on download AGAIN, but in the newest window (this is where I have the problem)
download_button = driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[3]/div/div[3]/button')
download_button.click()

尽管您的定位器可以工作,但请尝试将此定位器用于download_button:

download_button = driver.find_element_by_css_selector('div.f5 button')

最新更新