如何使用java

时间:2016-08-17 07:18:28

标签: java selenium

我的Html

<div id="example-1-tab-1" class="responsive-tabs-panel" style="display: block;">
  <div class="freme_box">
   <iframe class="demo-frame" src="dropdown/default.html">
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
     <head>
     <body>
      <select>
         <option selected="" value="">Please Select</option>
         <option value="Afghanistan">Afghanistan</option>
         <option value="Albania">Albania</option>
      </select>
     </body>
    </html>
   </iframe>
  </div>

我想从下拉列表中选择值,我已经使用了select方法,但它不起作用。获得:

  

未找到元素异常。

不确定如何处理不可见的元素。任何帮助?

1 个答案:

答案 0 :(得分:1)

实际上select元素位于iframe内,因为它在提供的HTML中看到,因此您需要先切换iframe,然后才能找到select元素,如下所示: -

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe.demo-frame")));

//Now find the dropdown 
Select select = new Select(driver.findElement(By.tagName("select"));

select.selectByVisibleText("Afghanistan");