Selenium webdriver将JSON对象设置为段落

时间:2017-03-01 19:02:41

标签: javascript json selenium

如何在p中的iframe标记中添加JSON对象?

我尝试了fllowing,但是广告文字“[object Object]”而不是来自对象的内容......

这是我的代码:

var arrJSON = [
  {
    "list": [
      {
        "pageRequest": "http://www.tmz.com/",
        "fullRequest": "http://0914.global.ssl.fastly.net/ad/img/x.gif?cb=1487451360453",
        "method": "GET",
        "contentType": "image/gif",
        "queryString": "cb=1487451360453",
        "queryDelimiter": ""
      }
    ],
    "tagName": "DNStination Inc fastly.net",
    "regex": "((ht|f)tp(s?)://)?(.*)fastly.net/ad/img/x.gif(.*)",
    "domain": "fastly.net",
    "logoUrl": "https://logo.clearbit.com/fastly.net"
  },
  {
    "list": [
      {
        "pageRequest": "https://moz.com/",
        "fullRequest": "https://092-obr-737.mktoresp.com/webevents/visitWebPage?_mchNc=1487448129811&_mchCn=&_mchId=092-OBR-737&_mchTk=_mch-moz.com-1487448129792-38969&_mchHo=moz.com&_mchPo=&_mchRu=%2F&_mchPc=https%3A&_mchVr=151&_mchHa=&_mchRe=&_mchQp=",
        "method": "GET",
        "contentType": "image/gif",
        "queryString": "_mchNc=1487448129811&_mchCn=&_mchId=092-OBR-737&_mchTk=_mch-moz.com-1487448129792-38969&_mchHo=moz.com&_mchPo=&_mchRu=%2F&_mchPc=https%3A&_mchVr=151&_mchHa=&_mchRe=&_mchQp=",
        "queryDelimiter": "&"
      }
    ],
    "tagName": "Marketo, Inc mktoresp.com",
    "regex": "((ht|f)tp(s?)://)?(.*)mktoresp.com/webevents/visitWebPage(.*)",
    "domain": "mktoresp.com",
    "logoUrl": ""
  }
];
var webdriver = require('selenium-webdriver'),
chrome = require('selenium-webdriver/chrome');

const util = require('util');
    By = webdriver.By,
    until = webdriver.until;
var driver = new webdriver.Builder()
    .forBrowser('chrome')
    .build();

driver.get('https://my_site.com');
for(i=0; i<arrJSON.length; i++){
  driver.get('https://my_site.com/page');
  driver.wait(until.titleIs('Title'), 2000);
  var element = driver.findElement(By.css('option[value=\'Tags\']')).click();
  var string1 = JSON.stringify(arrJSON[i]);
  driver.executeScript('document.querySelectorAll(\'iframe\')[0].contentDocument.querySelector(\'p\').innerHTML = ' + string1);
  driver.sleep(2000);
}

2 个答案:

答案 0 :(得分:0)

这是一个快速解决方案,在我的最终。因此,由于您已嵌套objects,因此您还需要JSON.stringify内部对象。因此,根据我的理解,你真正想要的是JSON.stringify(JSON.stringify(arrJSON[i]))或类似我在下面的内容:

driver.get('https://my_site.com');
for(i=0; i<arrJSON.length; i++){
  driver.get('https://my_site.com/page');
  driver.wait(until.titleIs('Title'), 2000);
  var element = driver.findElement(By.css('option[value=\'Tags\']')).click();
  var string1 = JSON.stringify(arrJSON[i]);
  driver.executeScript('document.querySelectorAll(\'iframe\')[0].contentDocument.querySelector(\'p\').innerHTML = ' + JSON.stringify(string1));
  driver.sleep(2000);
}

答案 1 :(得分:0)

另一个修复方法如下:

driver.executeScript('document.querySelectorAll(\'iframe\')[0].contentDocument.querySelector(\'p\').innerHTML = '" + string1 + "'");