Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

同一个方法在放在eggjs服务schedule 定时模块里面执行就可以获取的到inputElem,但是放到controller模块里面通过接口调用时获取不到呢,再express里面也可以成功调用,debug模式可能获取得到 #5280

Open
yetingmao opened this issue Dec 15, 2023 · 3 comments

Comments

@yetingmao
Copy link

在此输入你需要反馈的 Bug 具体信息(Bug in Detail):

使用webdriverio来进行自动化
这一块代码放在controller里面一直获取不到元素,
` const { ctx, app } = this;
// 第一步获取预测服务的信息

const browser = await remote({
  capabilities: {
    browserName: 'chrome',
    'goog:chromeOptions': {
      binary: path.join(__dirname, '../../chrome/Application/chrome.exe'),
      // args: process.env.CI ? ['headless', 'disable-gpu'] : []
    },
    'wdio:chromedriverOptions': { // or 'wdio:geckodriverOptions', 'wdio:edgedriverOptions'
      binary: path.join(__dirname, '../../chromedriver.exe'),
    }
  }
})
await browser.url('https://www.baidu.com/');
const inputElem = await browser.$(`//*[@id="kw"]`);
console.log(11111111111111, inputElem);
await inputElem.setValue('WebdriverIO');
this.ctx.body = 'Success';

}但是在schedule 定时模块里面执行就可以获取的到onst { remote } = require('webdriverio');
const path = require("path");
module.exports = (app) => {
return {
schedule: {
interval: '10s', // 30分钟间隔codee
type: 'worker', // 指定所有的 worker 都需要执行
disable: true,
},
async task(ctx) {
const browser = await remote({
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
binary: path.join(__dirname, '../../chrome/Application/chrome.exe'),
// args: process.env.CI ? ['headless', 'disable-gpu'] : []
},
'wdio:chromedriverOptions': { // or 'wdio:geckodriverOptions', 'wdio:edgedriverOptions'
binary: path.join(__dirname, '../../chromedriver.exe'),
}
}
})
await browser.url('https://www.baidu.com/')
const inputElem = await browser.$(//*[@id="kw"])
console.log(11111111111111, inputElem)
await inputElem.setValue('WebdriverIO')
},
};

},尝试在express服务里面调用也可以成功,const express = require('express')
const { remote } = require('webdriverio');
const path = require("path");
const app = express()
const port = 3000
app.get('/api', async (req, res) => {
const browser = await remote({
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
binary: path.join(__dirname, '/chrome/Application/chrome.exe'),
},
'wdio:chromedriverOptions': {
binary: path.join(__dirname, '/chromedriver.exe'),
},
},
});

try {
    await browser.navigateTo('https://www.baidu.com/');
    const inputElem = await browser.$(`//*[@id="kw"]`)
    if (!inputElem) {
        console.error('Element not found');
        return;
    }

    await inputElem.setValue('WebdriverIO');
    const submitBtn = await browser.$('//*[@id="su"]');
    await submitBtn.click();
} catch (error) {
    console.error('WebdriverIO Error:', error);
} finally {
    //  await browser.deleteSession();
}

})

app.listen(port, () => {
console.log(Example app listening on port ${port})
})并且在单独文件直接用node执行也可以调用成功const { remote } = require('webdriverio');

(async () => {
const browser = await remote({
capabilities: {
browserName: 'chrome',
'goog:chromeOptions': {
binary: './chrome/Application/chrome.exe',
// args: process.env.CI ? ['headless', 'disable-gpu'] : []
},
'wdio:chromedriverOptions': { // or 'wdio:geckodriverOptions', 'wdio:edgedriverOptions'
binary: './chromedriver.exe' // or 'geckodriver', 'msedgedriver'
}
}
})
await browser.url('https://www.baidu.com/')
const inputElem = await browser.$(//*[@id="kw"])
console.log(11111111111111,inputElem)
await inputElem.setValue('WebdriverIO')
// const submitBtn = await browser.$(//*[@id="su"])
// // browser.closeWindow();
// await submitBtn.click();
})()
`

可复现问题的仓库地址(Reproduction Repo)

[](

Node 版本号:

18.0.0

Eggjs 版本号:

2.37.0

相关插件名称与版本号(PlugIn and Name):

"webdriverio": "^8.26.1"

操作平台与版本号(Platform and Version):

window11 家庭版

@yetingmao
Copy link
Author

刚才试了一下在controller 执行 await this.app.runSchedule('test'); 也获取不到

@sinkhaha
Copy link
Contributor

controller是正常的吗,controller位置会不会搞错了,没按约定的目录形式导致没加载到controller

@yetingmao
Copy link
Author

控制器是正常的吗,控制器位置会不会搞错了,未按约定的目录格式导致未加载到控制器

没错,我之前用的selenium去实现的,这次换webdriverio,项目结构都没变,浏览器都能打开,页面也出来了,就是获取不到元素

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants