无法将对象转换为硒中的字符串
Can't convert object to string in selenium
在文档中这很简单,但是使用 selenium pack 从网站获取变量是有问题的(我现在需要这样做)。问题是 console.log() returns 它作为我想要的没有参数的对象(可以看出,客户端的数量恰好为 string/int (删除 / 后)。
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);
HTML 我要查找的页面和字符串
以及我从 console.log
中得到的
[ 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] ]
使用console.log(number.getText());
后的输出
ManagedPromise {
flow_:
ControlFlow {
propagateUnhandledRejections_: true,
activeQueue_:
TaskQueue {
name_: 'TaskQueue::3',
flow_: [Circular],
tasks_: [Object],
interrupts_: null,
pending_: null,
subQ_: null,
state_: 'new',
unhandledRejections_: Set {} },
taskQueues_: Set { [Object] },
shutdownTask_: null,
hold_:
Timeout {
_called: false,
_idleTimeout: 2147483647,
_idlePrev: [Object],
_idleNext: [Object],
_idleStart: 431,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: 2147483647 } },
stack_:
{ Task: WebElement.getText()
at thenableWebDriverProxy.schedule (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
at WebElementPromise.schedule_ (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:1921:25)
at WebElementPromise.getText (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:2185:17)
at getNumber (/home/soda/main.js:15:24)
at Object.<anonymous> (/home/soda/main.js:19:1)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3) name: 'Task' },
parent_: null,
callbacks_: null,
state_: 'pending',
handled_: false,
value_: undefined,
queue_: null }
number
是 WebElement
,当您使用 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);
.getText() returns 一个承诺,因此为了获得文本,您需要使用 .then();
从承诺中提取值
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.
});
在文档中这很简单,但是使用 selenium pack 从网站获取变量是有问题的(我现在需要这样做)。问题是 console.log() returns 它作为我想要的没有参数的对象(可以看出,客户端的数量恰好为 string/int (删除 / 后)。
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);
HTML 我要查找的页面和字符串
以及我从 console.log
中得到的[ 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] ]
使用console.log(number.getText());
后的输出ManagedPromise {
flow_:
ControlFlow {
propagateUnhandledRejections_: true,
activeQueue_:
TaskQueue {
name_: 'TaskQueue::3',
flow_: [Circular],
tasks_: [Object],
interrupts_: null,
pending_: null,
subQ_: null,
state_: 'new',
unhandledRejections_: Set {} },
taskQueues_: Set { [Object] },
shutdownTask_: null,
hold_:
Timeout {
_called: false,
_idleTimeout: 2147483647,
_idlePrev: [Object],
_idleNext: [Object],
_idleStart: 431,
_onTimeout: [Function],
_timerArgs: undefined,
_repeat: 2147483647 } },
stack_:
{ Task: WebElement.getText()
at thenableWebDriverProxy.schedule (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:816:17)
at WebElementPromise.schedule_ (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:1921:25)
at WebElementPromise.getText (/home/soda/node_modules/selenium-webdriver/lib/webdriver.js:2185:17)
at getNumber (/home/soda/main.js:15:24)
at Object.<anonymous> (/home/soda/main.js:19:1)
at Module._compile (module.js:573:32)
at Object.Module._extensions..js (module.js:582:10)
at Module.load (module.js:490:32)
at tryModuleLoad (module.js:449:12)
at Function.Module._load (module.js:441:3) name: 'Task' },
parent_: null,
callbacks_: null,
state_: 'pending',
handled_: false,
value_: undefined,
queue_: null }
number
是 WebElement
,当您使用 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);
.getText() returns 一个承诺,因此为了获得文本,您需要使用 .then();
从承诺中提取值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.
});