typescript 人偶师在内容更改后第二次点击相同id的标签失败

lmyy7pcs  于 2023-06-24  发布在  TypeScript
关注(0)|答案(1)|浏览(147)
    • bounty还有4天到期**。回答此问题可获得+200声望奖励。Ntshembo Hlongwane希望引起更多关注这个问题:我们高度赞赏这个问题的解决方案,花了几天时间试图找出根本问题

正在学习人偶师,所以我尝试用表格填充this site

    • 什么脚本**
  • 转到上面的链接
  • 单击“创建临时ID”
  • 填写表格
  • 点击继续
  • 内容变更
  • 再次单击“继续”(该按钮与第一个“继续”按钮具有相同ID值
    • 问题**
  • 当我第二次点击按钮“继续”按钮时,它与第一次工作的ID相同,它失败了
    • 脚本**
  1. import tesseract from 'node-tesseract-ocr';
  2. import Puppeteer from 'puppeteer';
  3. const config = {
  4. lang: 'eng', // default
  5. oem: 3,
  6. psm: 13,
  7. };
  8. export class AutomationController {
  9. public async CreateProfile(data: any) {
  10. try {
  11. const _data = {
  12. nationality: 'BRA',
  13. id: '0508228690083',
  14. title: 'Mr',
  15. firstName: 'Shaun',
  16. middleName: 'Maxwell',
  17. surname: 'Smith',
  18. dob: '15062001',
  19. gender: 'M',
  20. email: 'shaun@gmail.com',
  21. countryCode: '+27',
  22. mobileNumber: '0832567890',
  23. };
  24. const browser = await Puppeteer.launch({ headless: false });
  25. const page = await browser.newPage();
  26. await page.goto(
  27. 'https://self-service.wits.ac.za/psc/csprodonl/UW_SELF_SERVICE/SA/c/VC_OA_LOGIN_MENU.VC_OA_LOGIN_FL.GBL',
  28. { waitUntil: 'networkidle2', timeout: 0 }
  29. );
  30. // Click create temp id button
  31. await page.waitForSelector('#VC_OA_LOGIN_WRK_REGISTER');
  32. await page.click('#VC_OA_LOGIN_WRK_REGISTER');
  33. // Nationality field
  34. await page.waitForSelector('#VC_OA_LOGIN_WRK_COUNTRY');
  35. // await page.select('select[id="VC_OA_LOGIN_WRK_COUNTRY"]', 'AUS');
  36. // ID number field
  37. await page.waitForSelector('#VC_OA_LOGIN_WRK_NATIONAL_ID');
  38. const idInput = await page.$('#VC_OA_LOGIN_WRK_NATIONAL_ID');
  39. await idInput?.type(_data.id);
  40. await this.delay(4000);
  41. // Name Title field
  42. await page.waitForSelector('#VC_OA_LOGIN_WRK_NAME_PREFIX');
  43. await page.select(
  44. 'select[id="VC_OA_LOGIN_WRK_NAME_PREFIX"]',
  45. _data.title
  46. );
  47. await this.delay(4000);
  48. // First name field
  49. await page.waitForSelector(
  50. '#win0divVC_OA_LOGIN_WRK_FIRST_NAMEctrl > #VC_OA_LOGIN_WRK_FIRST_NAME'
  51. ); // VC_OA_LOGIN_WRK_FIRST_NAME
  52. const firstNameInput = await page.$('#VC_OA_LOGIN_WRK_FIRST_NAME');
  53. await firstNameInput?.type(_data.firstName);
  54. await this.delay(4000);
  55. // Middle name field
  56. await page.waitForSelector('#VC_OA_LOGIN_WRK_MIDDLE_NAME');
  57. const middleNameInput = await page.$('#VC_OA_LOGIN_WRK_MIDDLE_NAME');
  58. await middleNameInput?.type(_data.middleName);
  59. await this.delay(4000);
  60. // Surname field
  61. await page.waitForSelector('#VC_OA_LOGIN_WRK_LAST_NAME');
  62. const surnameInput = await page.$('#VC_OA_LOGIN_WRK_LAST_NAME');
  63. await surnameInput?.type(_data.surname);
  64. await this.delay(4000);
  65. // Gender field
  66. await page.waitForSelector('#VC_OA_LOGIN_WRK_SEX');
  67. await page.select('select[id="VC_OA_LOGIN_WRK_SEX"]', _data.gender);
  68. await this.delay(4000);
  69. // Email field
  70. await page.waitForSelector(
  71. '#win0divVC_OA_LOGIN_WRK_EMAIL_ADDRctrl > #VC_OA_LOGIN_WRK_EMAIL_ADDR'
  72. );
  73. const emailInput = await page.$(
  74. '#win0divVC_OA_LOGIN_WRK_EMAIL_ADDRctrl > #VC_OA_LOGIN_WRK_EMAIL_ADDR'
  75. );
  76. await emailInput?.type(_data.email);
  77. await this.delay(4000);
  78. // Mobile number
  79. await page.waitForSelector('#VC_OA_LOGIN_WRK_VC_PHONE_CELL_SS');
  80. const mobileInput = await page.$('#VC_OA_LOGIN_WRK_VC_PHONE_CELL_SS');
  81. await mobileInput?.type(_data.mobileNumber);
  82. await this.delay(4000);
  83. await page.waitForSelector(
  84. '#win0divVC_SEC_WRK_HTML_AREA_02 > div > div > img'
  85. );
  86. // Bypass security check
  87. const imgs = await page.$$eval(
  88. '#win0divVC_SEC_WRK_HTML_AREA_02 > div > div > img',
  89. (imgs) => imgs.map((img) => img.getAttribute('src'))
  90. );
  91. const securityPassword = this.getCharacterAfterSecondUnderscore(imgs);
  92. await page.waitForSelector('#VC_OA_LOGIN_WRK_VC_SEC_CODE');
  93. const securityField = await page.$('#VC_OA_LOGIN_WRK_VC_SEC_CODE');
  94. await securityField?.type(securityPassword);
  95. await this.delay(4000);
  96. // Continue button
  97. await page.waitForSelector('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  98. const continueBtn = await page.$('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  99. await continueBtn.click();
  100. this.delay(5000);
  101. // TODO - HANDLE IF SECURITY CHECK FAILED
  102. //THIS THE BUTTON THAT FAILS TO CLICK SECOND TIME
  103. // CONFIRM Details button
  104. const confirmBtn = await page.$('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  105. await confirmBtn.click();
  106. console.log(`Automation.controller - 136`, 'DONE');
  107. await this.delay(4000);
  108. } catch (error) {
  109. console.log(`Automation.controller - 137`, error);
  110. }
  111. }
  112. private delay(time) {
  113. return new Promise(function (resolve) {
  114. setTimeout(resolve, time);
  115. });
  116. }
  117. private getCharacterAfterSecondUnderscore(strings: string[]): string {
  118. const results: string[] = [];
  119. for (const string of strings) {
  120. const splittedString = string.split('_');
  121. results.push(splittedString[2]);
  122. }
  123. return results.join('');
  124. }
  125. }
mf98qq94

mf98qq941#

我在我的机器上试过这个,你的问题的答案很简单,你只是忘记在this.delay(5000);行的开头添加await关键字:

  1. ...
  2. // Continue button
  3. await page.waitForSelector('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  4. const continueBtn = await page.$('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  5. await continueBtn.click();
  6. await this.delay(5000); // YOU FORGOT TO ADD await HERE
  7. // TODO - HANDLE IF SECURITY CHECK FAILED
  8. //THIS THE BUTTON THAT FAILS TO CLICK SECOND TIME
  9. // CONFIRM Details button
  10. const confirmBtn = await page.$('#VC_OA_LOGIN_WRK_CONTINUE_PB');
  11. await confirmBtn.click();
  12. ...

相关问题