This is a question regarding Selenium...but also JS!
So please don't post answers in any other languages than JS.
I tried and failed to translate answers from other languages. If you know a tool that can automatically translate Selenium code to the JS equivalent and work, let me know.
I want to click on an item with selenium (selenium-webdriver, using nodejs) but it cannot find the desired element when I search for it by id.
I tried many different things like switching frames, but this kind of nesting really got the best of me.
I stripped the big html page down to the basics:
- my goal is selecting the a
-tag with the id action
<!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>
(Unfortunately when I paste that text into an html file as a minified example the second body
-tag stays empty, I don't know why.)
I'm trying something like this right now and getting the error:
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();
});
}