我在我的本地测试了这个,它似乎对 iframe 中的按钮工作得很好。这是代码
var webdriver = require('selenium-webdriver');
var driver = new webdriver.Builder().usingServer().withCapabilities({'browserName': 'chrome' }).build();
driver.get('file:///Users/../sampleFiles/sample-iframe.html');
driver.wait(webdriver.until.ableToSwitchToFrame(0)).then((d) => {
//*** SLEEP HERE
const button = webdriver.By.css(".Button");
driver.wait(webdriver.until.elementLocated(button)).then((btn) => {
btn.click();
btn.getTagName().then((tag) => {
console.log(tag);
});
});
});
我进入button
控制台
和经过测试的 Iframe HTML 是
<html lang="en"><head>
<meta charset="UTF-8">
<title>Example of HTML Iframe</title>
</head>
<body>
<iframe src="file:///Users/../sampleFiles/sample.html" width="300" height="200">
<html><head>
<title>Page Title</title>
</head>
<body>
<h1>This is a Heading</h1>
<p>This is a paragraph.</p>
<button id="ButtonID" class="Button">Click Me!</button>
</body></html>
</iframe>
</body></html>
检查你的driver.wait(until.elementLocated(dropdownElem))
行,好像有错字,把它改成
driver.wait(until.elementLocated(button ))
然后再试一次