简体   繁体   English

用Javascript中的Webdriver返回DOM元素

[英]Returning a DOM element with Webdriver in Javascript

How do I return a DOM Element with Webdriver in Javascript? 如何在Javascript中使用Webdriver返回DOM元素?

I am using the wd.js Javascript bindings: 我正在使用wd.js Javascript绑定:

require("wd")
  .remote("promiseChain")
  .init()
  .get("http://www.google.com")
  .elementById("mngb")
  .then(function(element) { console.log(element); });

I am getting this weird object that is not a standard DOM Element (for example I cannot get the HTML code out of it): 我得到的不是标准DOM元素的奇怪对象(例如,我无法从中获取HTML代码):

{ value: '0',
  browser: 
  { domain: null,
    _events: {},
    _maxListeners: 10,
    configUrl: 
  { protocol: 'http:',
    slashes: true,
    auth: null,
    host: '127.0.0.1:4444',
    port: '4444',
    hostname: '127.0.0.1',
    hash: null,
    search: '',
    query: {},
    pathname: '/wd/hub',
    path: '/wd/hub',
    href: 'http://127.0.0.1:4444/wd/hub' },
 sauceRestRoot: 'https://saucelabs.com/rest/v1',
 noAuthConfigUrl: 
  { protocol: 'http:',
    slashes: true,
    host: '127.0.0.1:4444',
    port: '4444',
    hostname: '127.0.0.1',
    hash: null,
    search: null,
    query: null,
    pathname: '/wd/hub',
    path: '/wd/hub',
    href: 'http://127.0.0.1:4444/wd/hub' },
 defaultCapabilities: 
  { browserName: 'firefox',
    version: '',
    javascriptEnabled: true,
    platform: 'ANY' },
 _httpConfig: 
  { timeout: undefined,
    retries: 3,
    retryDelay: 15,
    baseUrl: undefined },
 sampleElement: { value: 1, browser: [Circular] },
 sessionID: '238c9837-3d82-4d90-9594-cefb4ba8e6b9' } }

It is impossible to get the actual Javascript DOM element with any Webdriver client, but it should be no problem for most cases since the method getAttribute() is available. 使用任何Webdriver客户端都不可能获得实际的Javascript DOM元素,但是在大多数情况下应该没有问题,因为方法getAttribute()是可用的。 For example: 例如:

require("wd")
  .remote("promiseChain")
  .init()
  .get("http://www.google.com")
  .elementById("mngb")
  .getAttribute("outerHTML")
  .then(console.log.bind(console))

returns the HTML code for #mngb on www.google.com 返回www.google.com上#mngb的HTML代码

In case using the DOM element is really needed, one can create a copy using jsdom 如果确实需要使用DOM元素,则可以使用jsdom创建一个副本

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

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