獲得元素在下面我的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)
嘗試'elem2.click()'? – dilbert
不行,檢查我的編輯以查看異常。 – user3369157
這是模板頁嗎?即:'{%if request.user.is_authenticated or logged_in%}'?也許在服務頁面上嘗試此測試? – dilbert