简体   繁体   English

我如何完全解构这个 JavaScript 对象,它一直返回 Undefined 但有分配给它的值

[英]How do I destructure this JavaScript Object completely, It keeps returning Undefined yet there`s a value assigned to it

I`m using selenium to webscrape a website that is renderd dynamically using JavaScript but I am not get the data I am looking for, The initial data returned was an array of objects;我正在使用 selenium 抓取一个使用 JavaScript 动态呈现的网站,但我没有得到我正在寻找的数据,返回的初始数据是一个对象数组;

            [
              WebElement {
              driver_: thenableWebDriverProxy {
              session_: [Promise],
              executor_: [Executor],
              fileDetector_: null,
              onQuit_: [Function: onQuit],
              then: [Function: bound then],
              catch: [Function: bound catch]
            },
          id_: Promise { '4c778b4e-64aa-4433-ad98-2d07e9f20c29' }
       },

Which I further opened into;我进一步打开了它;

    thenableWebDriverProxy {
             session_: Promise {
                         Session {
                         caps_: [Capabilities]
                       }
                    },
             executor_: Executor {
                      w3c: true,
                      customCommands_: Map(4) {
                      'getContext' => [Object],
                      'setContext' => [Object],
                      'install addon' => [Object],
                      'uninstall addon' => [Object]
                   },
            log_: Logger {
                    name_: 'webdriver.http.Executor',
                   level_: null,
                   parent_: [Logger],
                   handlers_: null
               }
           },
          fileDetector_: null,
          onQuit_: [Function: onQuit],
         then: [Function: bound then],
         catch: [Function: bound catch]
  }

And this again contiues into;这再次延续到;

                    Promise {
                        Session {
                            id_: '4083ce6f-f536-41bd-9f4e-9c1a66c4486f',
                            caps_: Capabilities { map_: [Map] }
                       }
                    },

            

Now how do I get to the properties of caps_ that have been nested in there, My code keeps returning undefined even when I test for the value of id_ which should obviously return a value since I can see it even without writting any line of code.现在,我如何才能对已嵌套在那里caps_的性质,我的代码一直归国undefined ,甚至当我测试的价值id_这显然应该返回一个值,因为我可以看到它,即使没有书面方式的任何一行代码。

Here's my code这是我的代码

                         let {Builder, By} = require('selenium-webdriver');
                            driver = new Builder().forBrowser('firefox').build();

                            (async function test(){

                            await driver.get('https://www.betika.com/');

                            let data = driver.findElements(By.css('.match'));

                             try{
                                  data.then(function(result) {
                                  console.log(result);
                                  
                                  const fine = result.forEach(element => { 
        
                                         console.log(element.driver_); 
                                  });

                                  const outPut = result.forEach(element => {
                                         console.log(element.
                                                     driver_.
                                                     session_);

                                  });

                           const Test = result.forEach(element => {
                              console.log(element.
                                    driver_.
                                    session_.
                                    caps_);

                            });
                      

            

Actually "data" doens't work as a DOM tree, the library says that the return of function used in your code is a WebElement, so you can use to navigate or get information based on the search.实际上,“数据”并不像 DOM 树那样工作,库说您的代码中使用的函数的返回是一个 WebElement,因此您可以使用它来导航或基于搜索获取信息。

For exemple:举个例子:

let { Builder, By } = require('selenium-webdriver');

driver = new Builder().forBrowser('chrome').build();

async function test() {

    await driver.get('https://yoursite/login');

    let data = await driver.findElement(By.css('label')).getText();

    console.log(data)
}

test();

Data result could be something like "E-mail" if the site has a structure like this:如果站点具有如下结构,则数据结果可能类似于“电子邮件”:

<form>
<label>E-mail<input/><label>
<form>

Source: https://www.selenium.dev/documentation/webdriver/来源: https : //www.selenium.dev/documentation/webdriver/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM