簡體   English   中英

我如何完全解構這個 JavaScript 對象,它一直返回 Undefined 但有分配給它的值

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

我正在使用 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' }
       },

我進一步打開了它;

    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]
  }

這再次延續到;

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

            

現在,我如何才能對已嵌套在那里caps_的性質,我的代碼一直歸國undefined ,甚至當我測試的價值id_這顯然應該返回一個值,因為我可以看到它,即使沒有書面方式的任何一行代碼。

這是我的代碼

                         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_);

                            });
                      

            

實際上,“數據”並不像 DOM 樹那樣工作,庫說您的代碼中使用的函數的返回是一個 WebElement,因此您可以使用它來導航或基於搜索獲取信息。

舉個例子:

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();

如果站點具有如下結構,則數據結果可能類似於“電子郵件”:

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

來源: https : //www.selenium.dev/documentation/webdriver/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM