这是一个关于Selenium...but的问题,也是JS!
因此,除了JS之外,请不要用任何其他语言发布答案。
我试着翻译其他语言的答案,但都失败了。如果您知道一个工具可以自动将Selenium代码转换为JS等价物并工作,请告诉我。
我想用单击一个带有selenium (selenium-webdriver,使用nodejs)的项目,但是当我按id搜索它时,它找不到想要的元素。
我尝试了很多不同的东西,比如切换帧,但是这种嵌套确实让我得到了最好的结果。
我把html的大页面简化到了基本内容:
action选择a-tag 。<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<title>Selenium Test</title>
</head>
<body>
<div id="mainDiv">
<div id="appFrameContainer">
<iframe name="appFrame" id="appFrame" src="/longlink">
#document
<!DOCTYPE HTML>
<html>
<head></head>
<frameset>
<frameset id="frameset2">
<frame src="/longlink2" name="frame1" id="frame1">
#document
<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head></head>
<body>
<div id="menu">
<table class="table">
<tbody>
<tr>
<td>
<div>
<ul>
<li id="item">
<span>
<a id="action">
Execute Action
</a>
</span>
</li>
</ul>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
</frame>
</frameset>
</frameset>
</html>
</iframe>
</div>
</div>
</body>
</html>(不幸的是,当我将文本粘贴到html文件中作为一个缩小的示例时,第二个body-tag仍然是空的,我不知道为什么)。
我现在正在尝试这样的方法,并得到了错误:
UnhandledPromiseRejectionWarning: InvalidArgumentError: data did not match any variant of untagged enum FrameId at line 1 column 28
//Setup
const Capabilities = require('selenium-webdriver/lib/capabilities').Capabilities;
let capabilities = Capabilities.firefox();
capabilities.set('acceptInsecureCerts', true);
const firefox = 'firefox';
let driver = new Builder()
.forBrowser(firefox)
.withCapabilities(capabilities)
.build();
driver.get("<url>").then(startAutomation).catch();
//Process
async function startAutomation() {
let mainFrameName = "appFrame";
let idAction = "action";
driver.switchTo().frame(mainFrameName);
driver.findElement(By.id(idAction)).then(e => {
e.click();
});
}发布于 2019-05-02 18:01:43
请您检查下面的代码是否适合您(请单击具有id=“action”的锚标记)
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("document.getElementById('appFrame').contentWindow.document.getElementById('action').click();")https://stackoverflow.com/questions/55956818
复制相似问题