-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathdomOperationMock.js
105 lines (91 loc) · 2.78 KB
/
domOperationMock.js
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
/**
* 这里面都是模拟dom的操作
*/
const { Builder, By, until } = require("selenium-webdriver");
const chrome = require("selenium-webdriver/chrome");
// 全局 WebDriver 实例
let driver;
// 获取 WebDriver 实例
function getDriver() {
return driver;
}
/**
* 使用指定的选项打开浏览器
*/
async function openBrowserWithOptions(url, browser) {
const options = new chrome.Options();
options.addArguments("--detach");
if (browser === "chrome") {
driver = await new Builder()
.forBrowser("chrome")
.setChromeOptions(options)
.build();
await driver.manage().window().maximize();
} else {
throw new Error("不支持的浏览器类型");
}
await driver.get(url);
// 等待直到页面包含登录按钮dom
const xpathLocator = By.xpath("//*[@id='header']/div[1]/div[3]/div/a");
await driver.wait(until.elementLocated(xpathLocator), 10000);
}
// 点击登录按钮,并等待登录成功
async function logIn() {
// 点击登录
const loginButton = await driver.findElement(
By.xpath("//*[@id='header']/div[1]/div[3]/div/a")
);
await loginButton.click();
// 等待微信登录按钮出现
const xpathLocatorWechatLogin =
"//*[@id='wrap']/div/div[2]/div[2]/div[2]/div[1]/div[4]/a";
await driver.wait(
until.elementLocated(By.xpath(xpathLocatorWechatLogin)),
10000
);
const wechatButton = await driver.findElement(
By.xpath("//*[@id='wrap']/div/div[2]/div[2]/div[2]/div[1]/div[4]/a")
);
await wechatButton.click();
const xpathLocatorWechatLogo =
"//*[@id='wrap']/div/div[2]/div[2]/div[1]/div[2]/div[1]/img";
await driver.wait(
until.elementLocated(By.xpath(xpathLocatorWechatLogo)),
10000
);
// 登录成功
const xpathLocatorLoginSuccess = "//*[@id='header']/div[1]/div[3]/ul/li[2]/a";
await driver.wait(
until.elementLocated(By.xpath(xpathLocatorLoginSuccess)),
60000
);
}
// 根据索引获取职位描述
async function getJobDescriptionByIndex(index) {
try {
const jobSelector = `//*[@id='wrap']/div[2]/div[2]/div/div/div[1]/ul/li[${index}]`;
const jobElement = await driver.findElement(By.xpath(jobSelector));
// 点击招聘信息列表中的项
await jobElement.click();
// 找到描述信息节点并获取文字
const descriptionSelector =
"//*[@id='wrap']/div[2]/div[2]/div/div/div[2]/div/div[2]/p";
await driver.wait(
until.elementLocated(By.xpath(descriptionSelector)),
10000
);
const jobDescriptionElement = await driver.findElement(
By.xpath(descriptionSelector)
);
return jobDescriptionElement.getText();
} catch (error) {
console.log(`在索引 ${index} 处找不到工作。`);
return null;
}
}
module.exports = {
getDriver,
openBrowserWithOptions,
logIn,
getJobDescriptionByIndex,
};