无法在selenium中将对象转换为字符串

时间:2017-02-08 08:46:46

标签: javascript node.js selenium tostring

在文档中,这很简单,但使用selenium pack从网站获取变量是有问题的(因为我现在需要)。问题是console.log()将其作为没有我想要的参数的对象返回(可以看出,正好是客户端的数量为string / int(删除后/)。

Lib

我正在寻找的页面和字符串的HTML

HTML of the page (copying it to normal html code paste was taking quite long

What I see there and what number do I want

我从console.log获得的内容

var webdriver = require('selenium-webdriver'),
By = webdriver.By,
until = webdriver.until;

var driver = new webdriver.Builder()
.forBrowser('firefox')
.build();

var number;

driver.get('the website');
number = driver.findElement(By.xpath('/html/body/table/tbody/tr[4]/td[2]/table/tbody/tr/td[1]/table/tbody/tr[15]/td[2]')); //.toString is not working either, valueof too
console.log(number);

使用console.log(number.getText());

后的输出
[ thenableWebDriverProxy {
    flow_:
     ControlFlow {
       propagateUnhandledRejections_: true,
       activeQueue_: [Object],
       taskQueues_: [Object],
       shutdownTask_: null,
       hold_: [Object] },
    session_:
     ManagedPromise {
       flow_: [Object],
       stack_: null,
       parent_: [Object],
       callbacks_: [Object],
       state_: 'blocked',
       handled_: true,
       value_: undefined,
       queue_: null },
    executor_: Executor { w3c: false, customCommands_: [Object], log_: [Object] },
    fileDetector_: null,
    onQuit_: [Function: onQuit],
    cancel: [Function],
    then: [Function: bound then],
    catch: [Function: bound then] },
  ManagedPromise {
    flow_:
     ControlFlow {
       propagateUnhandledRejections_: true,
       activeQueue_: [Object],
       taskQueues_: [Object],
       shutdownTask_: null,
       hold_: [Object] },
    stack_: null,
    parent_: null,
    callbacks_: null,
    state_: 'fulfilled',
    handled_: false,
    value_: 'unused',
    queue_:
     TaskQueue {
       name_: 'TaskQueue::3',
       flow_: [Object],
       tasks_: [Object],
       interrupts_: null,
       pending_: null,
       subQ_: null,
       state_: 'new',
       unhandledRejections_: Set {} } },
  [Function],
  [Function: bound then],
  [Function: bound catch],
  [Function] ]

2 个答案:

答案 0 :(得分:2)

numberWebElement,当您使用toString()时,您会获得对象字符串,而不是文本。对于文本,使用getText()

number = driver.findElement(By.xpath('/html/body/table/tbody/tr[4]/td[2]/table/tbody/tr/td[1]/table/tbody/tr[15]/td[2]')).getText();
console.log(number);

答案 1 :(得分:0)

.getText()返回一个promise,所以为了获取文本,你需要使用.then();

从promise中提取值。
number = driver.findElement(By.xpath('/html/body/table/tbody/tr[4]/td[2]/table/tbody/tr/td[1]/table/tbody/tr[15]/td[2]')); //.toString is not working either, valueof too

number.then(function(text){
 console.log(text); //this will log the actual text.
});