Selenium getText没有后代

时间:2017-05-29 11:29:14

标签: javascript node.js selenium selenium-webdriver

有没有一种很好的方法来获取当前WebElement的文本,而后代不包含文本?

在网页中给出以下结构:

11:22:08

我想提取webdriver.findElement(By.css('div.bottom')).getText()

我正在使用selenium-webdriver 3.4.0在Node.JS中编写测试。 11:22:08 UTC会给我UTC。然后,我可以移除webdriver.findElement(By.css('div.bottom span')).getText()子字符串:const getTime = () => { const timeElements = $('.nav-wrapper li div.bottom'); return timeElements.contents().not(timeElements.children()).text(); // get rid of SPAN with timezone };

然而,我正在寻找一些更优雅的解决方案。即在jQuery中我能做到:

var canvasDots = function() {
  var canvas = document.querySelector('canvas'),
      ctx = canvas.getContext('2d'),
      colorDot = '#00bdbf',
      color = '#00bdbf';
  canvas.width = window.innerWidth;
  canvas.height = window.innerHeight;
  canvas.style.display = 'block';
  ctx.fillStyle = colorDot;
  ctx.lineWidth = .1;
  ctx.strokeStyle = color;

  var mousePosition = {
    x: 30 * canvas.width / 100,
    y: 30 * canvas.height / 100
  };

  var dots = {
    nb: 350,
    distance: 60,
    d_radius: 100,
    array: []
  };

  function Dot(){
    this.x = Math.random() * canvas.width;
    this.y = Math.random() * canvas.height;

    this.vx = -.5 + Math.random();
    this.vy = -.5 + Math.random();

    this.radius = Math.random();
  }

  Dot.prototype = {
    create: function(){
      ctx.beginPath();
      ctx.arc(this.x, this.y, this.radius, 0, Math.PI * 2, false);
      ctx.fill();
    },

    animate: function(){
      for(i = 0; i < dots.nb; i++){

        var dot = dots.array[i];

        if(dot.y < 0 || dot.y > canvas.height){
          dot.vx = dot.vx;
          dot.vy = - dot.vy;
        }
        else if(dot.x < 0 || dot.x > canvas.width){
          dot.vx = - dot.vx;
          dot.vy = dot.vy;
        }
        dot.x += dot.vx;
        dot.y += dot.vy;
      }
    },

    line: function(){
      for(i = 0; i < dots.nb; i++){
        for(j = 0; j < dots.nb; j++){
          i_dot = dots.array[i];
          j_dot = dots.array[j];

          if((i_dot.x - j_dot.x) < dots.distance && (i_dot.y - j_dot.y) < dots.distance && (i_dot.x - j_dot.x) > - dots.distance && (i_dot.y - j_dot.y) > - dots.distance){
            if((i_dot.x - mousePosition.x) < dots.d_radius && (i_dot.y - mousePosition.y) < dots.d_radius && (i_dot.x - mousePosition.x) > - dots.d_radius && (i_dot.y - mousePosition.y) > - dots.d_radius){
              ctx.beginPath();
              ctx.moveTo(i_dot.x, i_dot.y);
              ctx.lineTo(j_dot.x, j_dot.y);
              ctx.stroke();
              ctx.closePath();
            }
          }
        }
      }
    }
  };

  function createDots(){
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    for(i = 0; i < dots.nb; i++){
      dots.array.push(new Dot());
      dot = dots.array[i];

      dot.create();
    }

    dot.line();
    dot.animate();
  }

  window.onmousemove = function(parameter) {
    mousePosition.x = parameter.pageX;
    mousePosition.y = parameter.pageY;
  }

  mousePosition.x = window.innerWidth / 2;
  mousePosition.y = window.innerHeight / 2;

  setInterval(createDots, 1000/30); 
};

window.onload = function() {
  canvasDots();
};

有人可以回答我应该删除空格字符后面的所有内容,但是这个问题指的是提取文本而不包含在后代中的文本的一般问题&#39;标签

1 个答案:

答案 0 :(得分:0)

尝试使用JavascriptExecutor获取所需的输出,如下所示:

WebElement element=driver.findElement(By.xpath("//div[@class='bottom']"));
JavascriptExecutor jse = (JavascriptExecutor)driver;
String time = (String) jse.executeScript("return arguments[0].childNodes[0].nodeValue;", element);