-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_colab_runner.js
More file actions
151 lines (130 loc) · 5.06 KB
/
test_colab_runner.js
File metadata and controls
151 lines (130 loc) · 5.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// 简单的 Colab 代码执行测试脚本
// 直接在 Colab 页面的控制台中粘贴运行
(function() {
console.log('=== Colab 代码执行测试开始 ===');
// 测试方法1: 查找运行按钮
function testRunButtons() {
console.log('\n--- 测试方法1: 查找运行按钮 ---');
const selectors = [
'button[aria-label*="Run cell"]',
'button[aria-label*="Run"]',
'button[title*="Run"]',
'colab-run-button',
'paper-icon-button[icon="av:play-arrow"]',
'button[data-tooltip*="Run"]'
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
console.log(`选择器 "${selector}": 找到 ${elements.length} 个元素`);
if (elements.length > 0) {
console.log('第一个元素:', elements[0]);
}
}
}
// 测试方法2: 查找代码单元
function testCodeCells() {
console.log('\n--- 测试方法2: 查找代码单元 ---');
const selectors = [
'.code-cell',
'[data-type="code"]',
'.cell.code',
'div[class*="cell"]',
'.notebook-cell'
];
for (const selector of selectors) {
const elements = document.querySelectorAll(selector);
console.log(`选择器 "${selector}": 找到 ${elements.length} 个元素`);
if (elements.length > 0) {
console.log('第一个元素:', elements[0]);
}
}
}
// 测试方法3: 查找所有可能的执行相关元素
function testAllExecutionElements() {
console.log('\n--- 测试方法3: 查找所有执行相关元素 ---');
const allButtons = document.querySelectorAll('button, paper-icon-button, iron-icon, [role="button"]');
let found = 0;
allButtons.forEach((button, index) => {
const text = (button.textContent || '').toLowerCase();
const ariaLabel = (button.getAttribute('aria-label') || '').toLowerCase();
const title = (button.getAttribute('title') || '').toLowerCase();
const icon = button.getAttribute('icon') || '';
if (text.includes('run') || ariaLabel.includes('run') || title.includes('run') ||
text.includes('执行') || ariaLabel.includes('执行') || title.includes('执行') ||
icon.includes('play')) {
console.log(`找到执行相关按钮 ${found++}:`, {
element: button,
text: text,
ariaLabel: ariaLabel,
title: title,
icon: icon
});
}
});
console.log(`总共找到 ${found} 个可能的执行按钮`);
}
// 实际执行第一个代码单元
function executeFirstCell() {
console.log('\n--- 尝试执行第一个代码单元 ---');
// 先刷新页面
console.log('🔄 正在刷新页面...');
location.reload();
// 等待页面刷新完成后执行
setTimeout(() => {
console.log('✅ 页面已刷新,开始查找运行按钮...');
// 方法1: 查找并点击第一个运行按钮
const runSelectors = [
'button[aria-label*="Run"]',
'colab-run-button',
'paper-icon-button[icon="av:play-arrow"]'
];
for (const selector of runSelectors) {
const buttons = document.querySelectorAll(selector);
if (buttons.length > 0) {
console.log(`使用选择器 "${selector}" 找到运行按钮,尝试点击...`);
buttons[0].click();
console.log('✅ 点击成功!');
return true;
}
}
// 方法2: 查找代码单元并发送快捷键
const cellSelectors = ['.code-cell', '[data-type="code"]', '.cell'];
for (const selector of cellSelectors) {
const cells = document.querySelectorAll(selector);
if (cells.length > 0) {
console.log(`使用选择器 "${selector}" 找到代码单元,尝试发送 Shift+Enter...`);
const firstCell = cells[0];
firstCell.focus();
firstCell.click();
setTimeout(() => {
const event = new KeyboardEvent('keydown', {
key: 'Enter',
shiftKey: true,
bubbles: true,
cancelable: true
});
firstCell.dispatchEvent(event);
document.dispatchEvent(event);
console.log('✅ 快捷键发送成功!');
}, 100);
return true;
}
}
console.log('❌ 所有方法都失败了');
return false;
}, 2000); // 等待2秒让页面完全加载
}
// 运行所有测试
testRunButtons();
testCodeCells();
testAllExecutionElements();
console.log('\n=== 测试完成,现在尝试执行 ===');
executeFirstCell();
console.log('\n=== 如果想手动执行第一个代码单元,请运行: ===');
console.log('executeFirstCell()');
// 将函数暴露到全局作用域
window.executeFirstCell = executeFirstCell;
window.testRunButtons = testRunButtons;
window.testCodeCells = testCodeCells;
window.testAllExecutionElements = testAllExecutionElements;
})();