【问题标题】:How to solve Dynamic DOM problem when iterating through URLs?遍历 URL 时如何解决动态 DOM 问题?
【发布时间】:2020-11-20 11:26:26
【问题描述】:

我想做以下事情:

  1. 打开一个网址
  2. 点击下载
  3. 使用新网址打开新标签页
  4. 关闭上一个标签
  5. 点击新网址(新标签)上的下载
  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:\users\i539797\appdata\local\programs\python\python38-32\lib\site-packages\selenium\webdriver\remote\webelement.py in click(self)
     78     def click(self):
     79         """Clicks the element."""
---> 80         self._execute(Command.CLICK_ELEMENT)
     81 
     82     def submit(self):

c:\users\i539797\appdata\local\programs\python\python38-32\lib\site-packages\selenium\webdriver\remote\webelement.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:\users\i539797\appdata\local\programs\python\python38-32\lib\site-packages\selenium\webdriver\remote\webdriver.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:\users\i539797\appdata\local\programs\python\python38-32\lib\site-packages\selenium\webdriver\remote\errorhandler.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。

【问题讨论】:

    标签: python html css selenium loops


    【解决方案1】:

    在调用.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')
    

    【讨论】:

    • 非常感谢!!有效。但是有一个问题:每当我打开一个新标签时,它都会给我“元素点击被拦截”错误。我相信这是因为您第一次在同一浏览器上打开网站时会出现一个“隐藏”窗口。当我尝试再次单击下载按钮(在新选项卡上)时,它给了我错误。关于如何解决这个问题的任何想法?我在这里发了一个帖子:stackoverflow.com/questions/63193117/…
    • @LuizScheuer 这个问题已经被其他用户解决了,很好。抱歉,我刚回到我的笔记本电脑。
    猜你喜欢
    • 1970-01-01
    • 2015-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多