我是Selenium WebDriver测试的新手,我尝试在工作中使用它。我尝试了许多选择器,xpath等组合,但我无法超越它。我在stackoverflow上搜索了许多类似的主题,遗憾的是没有预期的结果。我需要的是能够点击“NO SERVICE”按钮(一个href)。当我尝试时,我不断收到错误,这个元素不可见。当我尝试使用“wait”来处理此错误时,我不断收到另一个错误“预期条件失败:等待元素的可见性......”。我做错了什么?
我的代码:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.className("withoutService")));
WebElement x = driver.findElement(By.className("withoutService"));
x.click();
以及来自网页的html代码段:
<div id="fancybox-outer">
<div id="fancybox-content">
<div style="width:auto;position:relative;">
<div id="serviceReminder" style="width: 765px">
<form id="serviceReminderFrom" method="post">
<div class="homeMessage">
<div class="innerMessage">
<input type="hidden" id="serviceToAddReminderFromAction" name="F_ACTION" value="">
<input type="hidden" id="itemsWithServices" name="itemsWithServices" value="">
<input type="hidden" name="eventTypeName" value="Something">
<div class="ServicesDelivery"><span class="disable-button"></span>
<a href="javaScript:void(0);" rel="3" class="withoutService btn btn-fourth" onclick="registerButtonClickOnPopup('NO SERVICE'); setTimeout(function(){registerButtonClickOnPopup('NO SERVICE');},400);">NO SERVICE</a>
<a href="javascript:void(0)" rel="1" class="next js-tooltip btn btn-second" onclick="registerButtonClickOnPopup('ADD SERVICE'); setTimeout(function(){registerButtonClickOnPopup('ADD SERVICE');},400);">ADD SERVICE</a>
<div class="none">
</div>
<div class="clear"></div>
</div>
</div>
</div>
</form>
</div></div></div><a id="fancybox-close" style="display: inline;"></a><div id="fancybox-title" class="" style="display: none;">
</div><a href="javascript:;" id="fancybox-left"><span class="fancy-ico" id="fancybox-left-ico"></span></a><a href="javascript:;" id="fancybox-right"><span class="fancy-ico" id="fancybox-right-ico"></span></a></div>
答案 0 :(得分:0)
您的定位器By.className("withoutService")
可以匹配多个元素。您需要更具体的选择器。请尝试以下代码:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
WebElement x = waitWait.until(ExpectedConditions.elementToBeClickable(By.linkText("NO SERVICE")));
x.click();
答案 1 :(得分:0)
尝试以下XPath:
//a[contains(@class, 'withoutService')]
完整代码:
WebDriverWait waitWait = new WebDriverWait(driver, 40);
WebElement x = waitWait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[contains(@class, 'withoutService')]")));
x.click();
如果上面的代码不起作用,那么元素可能在iframe
内。请查看我的详细答案here。