2017-02-14 177 views
2

我是Selenium WebDriver测试的新手,我尝试在工作中使用它。我尝试了许多选择器,xpaths等的组合,但我无法移过它。我也在stackoverflow上搜索了很多类似的主题,可惜没有预期的结果。我需要的是能够点击“无服务”按钮(一个href)。当我尝试时,我不断收到错误,说明此元素不可见。当我尝试使用“等待”处理此错误时,我不断收到另一个错误“预期的条件失败:等待元素的可见性...”。我究竟做错了什么?Selenium WebDriver Java - 元素不可见

我的代码:

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

你正在寻找的元素包含在“隐藏”的输入内,你在测试中做了些什么来使输入不被隐藏? – Josh

+0

@Josh,不,它不是隐藏的“输入”的一部分。 AFAIK,'input'元素只能包含属性,而不能包含其他节点。 – Andersson

回答

0

你的定位By.className("withoutService")可以匹配多种元素。你需要更具体的选择器。尝试下面的代码:

WebDriverWait waitWait = new WebDriverWait(driver, 40);  
WebElement x = waitWait.until(ExpectedConditions.elementToBeClickable(By.linkText("NO SERVICE")));  
x.click(); 
+0

我不认为他收到的错误表明这是一个问题,满足条件 – Josh

+0

多个元素对不起,我错误地编辑了你的答案。你可以拒绝它,如果你想 –

+1

@Naveen,没关系:)但是你应该注意到,如果元素位于'iframe'内,可能会得到'NoSuchElementException',但不是'ElementNotVisibleException' – Andersson

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

+1

实际上'withoutService'是完整的类名,可以与搜索'By.className()'一起使用。我认为,你混淆*完整*和*化合物*类名 – Andersson

+0

是的,我的坏。感谢您指出。 –

相关问题