如何使用html按钮运行javascript selenium函数



此代码运行良好,但我需要在html按钮上运行此脚本,这意味着当单击按钮时,将执行此函数!!!

async function example() {
let driver = await new Builder().forBrowser("chrome").build();
await driver.get("https://google.com");  
}
example();

我试过了,但没用,

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Javascript Testing</title>
</head>
<body>
<script type="text/Javascript">
async function example()
{
const {Builder, By, Key, util} = require("selenium-webdriver")
let driver = new Builder().forBrowser("chrome").build();
driver.get("https://google.com");
driver.findElement(By.name("q")).sendKeys("Selenium",KEY.RETURN);
}
</script>
<button onClick="example()">Click me</button>
</body>
</html>

错误:

index.html Uncaught ReferenceError: example is not defined
at HTMLButtonElement.onclick

而且,我也用外部js尝试过,但它仍然不能在中工作

selenium网络驱动程序无法在web浏览器中运行,它所需的API不可用。

您需要重写JS,使其在Node.JS.中运行

如果你想从网络浏览器访问它,那么你需要为它编写一个网络服务前端

然后,您可以使用例如fetch从浏览器访问该web服务。

最新更新