2014-04-21 56 views
0

获得元素在下面我的html代码:使用硒通过ID在Python测试

<div class="collapse navbar-collapse" id="b-menu-1"> 
        <ul class="nav navbar-nav navbar-right"> 


         <li><a href="/accounts/login/">Sign In</a></li> 
         <li><a href="/accounts/signup/">Sign Up</a></li> 


         {% if request.user.is_authenticated or logged_in %} 
         <li class="dropdown"> 
          <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
           <span class="glyphicon glyphicon-user"></span><b class="caret"></b></a> 

          <ul class="dropdown-menu"> 
           <li><a href="/accounts/manageAccount/">Manage Account</a></li> 
           <li><a href="/accounts/createProfile/">create profile</a></li> 
           <li><a href="/accounts/viewProfile/">view profile</a></li> 
           <li><a href="/accounts/editProfile/">edit profile</a></li> 
           <li><a href="/accounts/logout/" id="logout">Log out</a></li> 
          </ul> 
         </li> 


         <li data-toggle="modal" data-target="#my-modal-box" class="active"> 
          <a href="#"><span class="glyphicon glyphicon-search"> Search</a></li> 
        </ul> 
       </div> 

我要选择退出按钮,这实际上是出现在我的导航栏中选择。

我试图通过名称,id等获取元素,但没有解决。

elem2 = self.driver.find_element_by_id("logout") 
elem2.send_keys(Keys.RETURN) 

我明确地在注销的href链接中添加了id,但无法获取该元素。任何建议我怎样才能注销元素

我得到以下异常:

NoSuchElementException: Message: u'Unable to locate element: {"method":"id","selector":"logout"}' ; Stacktrace: 
    at FirefoxDriver.prototype.findElementInternal_ (file:///tmp/tmpPHM5S7/extensions/[email protected]/components/driver_component.js:8905) 
    at FirefoxDriver.prototype.findElement (file:///tmp/tmpPHM5S7/extensions/[email protected]/components/driver_component.js:8914) 
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmpPHM5S7/extensions/[email protected]/components/command_processor.js:10884) 
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmpPHM5S7/extensions/[email protected]/components/command_processor.js:10889) 
    at DelayedCommand.prototype.execute/< (file:///tmp/tmpPHM5S7/extensions/[email protected]/components/command_processor.js:10831) 
+0

尝试'elem2.click()'? – dilbert

+0

不行,检查我的编辑以查看异常。 – user3369157

+0

这是模板页吗?即:'{%if request.user.is_authenticated or logged_in%}'?也许在服务页面上尝试此测试? – dilbert

回答

0

您是否尝试过使用从UI支持Select(类selenium.webdriver得到这个.support.select.Select(webelement))?

我不知道Python的很好,所以我的例子是在Java中:

WebElement dropDownMenu = webDriver.findElement(By.xpath("//ul[@class='dropdown-menu']"); 
    Select logout = new Select(dropDownMenu); 
    logout.selectByVisibleText("Log out");