我已经仔细研究了所有以前的解决方案,但似乎无济于事。你是我唯一的希望。任何建议将不胜感激。谢谢。
我正在为我们的站点编写一些早就应该进行的集成测试。这项评估提出了数百个问题,每个问题都要求用户使用单选按钮从x个答案中选择一个。当我单击这些单选按钮之一时,出现错误:
org.openqa.selenium.ElementNotVisibleException: element not interactable (Session info: chrome=74.0.3729.169) (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows
...
我尝试更新到最新的ChromeDriver和Selenium,添加等待时间,验证我的按钮是否正确等等。
我也尝试了Gecko驱动程序并得到此错误:
org.openqa.selenium.ElementNotInteractableException:元素 无法滚动到视图中
我正在浏览的HTML(的一部分):
<td width="100%" align="left">
<table cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td valign="middle"><span class="radio"
style="background-position: 0px -80px;"></span><input
tabindex="5" type="radio"
style="align: center; vertical-align: middle;"
class="styled" name="PID9.QID1.RID1" id="PID9.QID1.RID1"
value="6"></td>
<td valign="middle"><span class="std_size2"> </span></td>
<td valign="middle"><span class="std_size2">Married</span></td>
</tr>
</tbody>
</table>
</td>
测试代码(包括我评论过的一些其他尝试):
private String doAssesment()
{
assertTrue(driver.getTitle().equals("Couple Assessment"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement decisionTree = driver.findElement(By.name("decision_tree"));
List<WebElement> inputs = decisionTree.findElements(By.tagName("input"));
for (WebElement button : inputs)
{
System.out.println("type=" + button.getAttribute("type"));
if (button.getAttribute("type").equals("radio"))
{
// wait.until(ExpectedConditions.visibilityOf(button));
// new Actions(driver).moveToElement(button).perform();
button.click();
break;
}
}
return driver.getTitle();
}
我看到的一个可能的问题是单选按钮注入了onchange javascript(这是对吗?)。
Javascript:
var Custom = {
init: function() {
var inputs = document.getElementsByTagName("input"),
span = Array(),
textnode, option, active;
for (a = 0; a < inputs.length; a++) {
if ((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
span[a] = document.createElement("span");
span[a].className = inputs[a].type;
if (inputs[a].checked == true) {
if (inputs[a].type == "checkbox") {
position = "0 -" + (checkboxHeight * 2) + "px";
span[a].style.backgroundPosition = position;
} else {
position = "0 -" + (radioHeight * 2) + "px";
span[a].style.backgroundPosition = position;
}
}
inputs[a].parentNode.insertBefore(span[a], inputs[a]);
inputs[a].onchange = Custom.clear;
span[a].onmousedown = Custom.pushed;
span[a].onmouseup = Custom.check;
document.onmouseup = Custom.clear;
}
}
inputs = document.getElementsByTagName("select");
for (a = 0; a < inputs.length; a++) {
if (inputs[a].className == "styled") {
option = inputs[a].getElementsByTagName("option");
active = option[0].childNodes[0].nodeValue;
textnode = document.createTextNode(active);
for (b = 0; b < option.length; b++) {
if (option[b].selected == true) {
textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
}
}
span[a] = document.createElement("span");
span[a].className = "select";
span[a].id = "select" + inputs[a].name;
span[a].appendChild(textnode);
inputs[a].parentNode.insertBefore(span[a], inputs[a]);
inputs[a].onchange = Custom.choose;
}
}
},
pushed: function() {
element = this.nextSibling;
if (element.checked == true && element.type == "checkbox") {
this.style.backgroundPosition = "0 -" + checkboxHeight * 3 + "px";
} else if (element.checked == true && element.type == "radio") {
this.style.backgroundPosition = "0 -" + radioHeight * 3 + "px";
} else if (element.checked != true && element.type == "checkbox") {
this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
} else {
this.style.backgroundPosition = "0 -" + radioHeight + "px";
}
},
check: function() {
element = this.nextSibling;
if (element.checked == true && element.type == "checkbox") {
this.style.backgroundPosition = "0 0";
element.checked = false;
} else {
if (element.type == "checkbox") {
this.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
} else {
this.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
group = this.nextSibling.name;
inputs = document.getElementsByTagName("input");
for (a = 0; a < inputs.length; a++) {
if (inputs[a].name == group && inputs[a] != this.nextSibling) {
inputs[a].previousSibling.style.backgroundPosition = "0 0";
}
}
}
element.checked = true;
}
},
clear: function() {
inputs = document.getElementsByTagName("input");
for (var b = 0; b < inputs.length; b++) {
if (inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight * 2 + "px";
} else if (inputs[b].type == "checkbox" && inputs[b].className == "styled") {
inputs[b].previousSibling.style.backgroundPosition = "0 0";
} else if (inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight * 2 + "px";
} else if (inputs[b].type == "radio" && inputs[b].className == "styled") {
inputs[b].previousSibling.style.backgroundPosition = "0 0";
}
}
},
choose: function() {
option = this.getElementsByTagName("option");
for (d = 0; d < option.length; d++) {
if (option[d].selected == true) {
document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
}
}
}
}
window.onload = Custom.init;
<
/script>
答案 0 :(得分:0)
结果表明单选按钮处于“隐藏”按钮的范围内。更改了代码以单击跨度,它似乎可以正常工作。
private String doAssesment()
{
assertTrue(driver.getTitle().equals("Couple Assessment"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
WebElement decisionTree = driver.findElement(By.name("decision_tree"));
List<WebElement> inputs = decisionTree.findElements(By.className("radio"));
WebElement button = inputs.get(4);
button.click();
return driver.getTitle();
}