我想與退出按鈕進行交互,但我似乎無法使用鏈接文本或XPath點擊它。無法點擊元素鏈接在硒蟒
我曾嘗試以下這些問題的答案,沒有運氣:
這裏是我的代碼:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from xatu.tests.base import login
from xatu.tests.bagon import BagonBaseTestCase
class BasicTestCase(BagonBaseTestCase):
@login
def test_logout(self):
self._wait_until_id_presents("quotes-form")
WebDriverWait(self, 10).until(
EC.presence_of_element_located((By.XPATH, "//a[@href='/login/clear']/i")))
self.browser.find_element_by_xpath("//a[@href='/login/clear']/i").click()
self.implicitly_wait(2)
self._title_check("Login")
第一行下test_logout調用函數等待某個元素出現在我們身上因此我可以看到該頁面已被加載。然後我嘗試點擊退出按鈕。
這是HTML(元件是在類= 「BTN的」 href = 「/登錄/清除」):
<div class="navbar navbar-fixed-top screen-only">
<div class="navbar-inner">
<div class="container-fluid">
<a class="btn btn-navbar" data-target=".nav-collapse" data-toggle="collapse">
<a class="brand" href="/">
<div class="nav-collapse collapse">
<ul class="nav">
<p class="pull-right" style="margin-top: 4px; margin-left: 4px;">
<a class="btn" href="/login/clear">
<i class="icon-off"/>
Sign out
</a>
</p>
<p class="navbar-text pull-right"> Logged-in as D. V. Lauper </p>
</div>
<!--/.nav-collapse -->
</div>
</div>
</div>
當嘗試通過發現LINK_TEXT,該元件不能被發現。運行這段代碼給了我一個堆棧跟蹤錯誤說:
ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
編輯:我已經試過Saifur的答案,並更新了我的代碼,以他的答案,但現在我得到:AttributeError的:「BasicTestCase」對象有沒有屬性「find_element」。我已經嘗試在WebDriverWait()中將「self」更改爲「self.browser」作爲參數,但是然後我會得到我的原始錯誤。
謝謝您的回答。儘管如此,我仍然遇到了錯誤, AttributeError:'BasicTestCase'對象沒有屬性'find_element'。我試圖將WebDriverWait中的「自我」更改爲「self.browser」,但我會得到我原來的錯誤。另外我相信你錯過了一個額外的括號。 – AlphaFoxtrot 2015-01-12 15:45:35
謝謝你的提醒,我糾正了''''。而且,你應該在'WebDriverWait(self,10)'裏面傳遞驅動實例,不管它是什麼,請確保你已經導入了正確的引用。見[this](https://selenium-python.readthedocs.org/waits。html#explicit-waits) – Saifur 2015-01-12 15:52:17
你還在獲得'exception'嗎? – Saifur 2015-01-12 16:17:06