我的应用程序从数据库中获取 ID 列表。我用游标遍历这些,对于每个 ID,我将它插入带有 Selenium 的 URL 以获取页面上的特定项目。这是对关键字进行搜索并获得与该搜索最相关的项目。数据库中有大约1000 个结果。在随机迭代中,驱动程序中的 1 个操作将抛出一个StaleElementReferenceError完整的消息:过时的元素引用:元素未附加到页面文档\n(会话信息:chrome=77.0.3865.75)查看官方文档,我可以看到导致此问题的两个常见原因是:该元素已被完全删除。该元素不再附加到 DOM。前者是最常见的原因。index.jsconst { MongoClient, ObjectID } = require('mongodb')const fs = require('fs')const path = require('path')const { Builder, Capabilities, until, By } = require('selenium-webdriver')const chrome = require('selenium-webdriver/chrome')require('dotenv').config()async function init() { try { const chromeOpts = new chrome.Options() const ids = fs.readFileSync(path.resolve(__dirname, '..', 'data', 'primary_ids.json'), 'utf8') const client = await MongoClient.connect(process.env.DB_URL || 'mongodb://localhost:27017/test', { useNewUrlParser: true }) const db = client.db(process.env.DB_NAME || 'test') const productCursor = db.collection('product').find( { accountId: ObjectID(process.env.ACCOUNT_ID), primaryId: { $in: JSON.parse(ids) } }, { _id: 1, primaryId: 1 } ) const resultsSelector = 'body #wrapper div.src-routes-search-style__container--2g429 div.src-routes-search-style__products--3rsz9' const mostRelevantSelector = `${resultsSelector} > div:nth-child(2) } }为了调试,我try...catch在每个驱动程序操作周围放置了一个,以查看它是哪个特定操作失败但不起作用,因为它从来都不是失败的一致操作。例如,有时如果本来是其中elementLocated一行或其他行,则它只是getAttribute动作。如果在那种情况下是后者,这就是为什么我很困惑为什么会抛出这个错误,因为 selenium 肯定已经在页面上找到了元素(即link)但无法getAttribute('src')在元素上做?这就是为什么我对我得到的错误感到困惑。我想我在设置 selenium 以处理迭代的方式上一定做错了。迭代次数永远不会超过110
添加回答
举报
0/150
提交
取消