2015-01-09 110 views
2

我想與退出按鈕進行交互,但我似乎無法使用鏈接文本或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」作爲參數,但是然後我會得到我的原始錯誤。

回答

1

使用explicit wait和相對xpath

//a[@href='/login/clear']/i

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.browser.implicitly_wait(2) 
     self._title_check("Login") 
+0

謝謝您的回答。儘管如此,我仍然遇到了錯誤, AttributeError:'BasicTestCase'對象沒有屬性'find_element'。我試圖將WebDriverWait中的「自我」更改爲「self.browser」,但我會得到我原來的錯誤。另外我相信你錯過了一個額外的括號。 – AlphaFoxtrot 2015-01-12 15:45:35

+0

謝謝你的提醒,我糾正了''''。而且,你應該在'WebDriverWait(self,10)'裏面傳遞驅動實例,不管它是什麼,請確保你已經導入了正確的引用。見[this](https://selenium-python.readthedocs.org/waits。html#explicit-waits) – Saifur 2015-01-12 15:52:17

+0

你還在獲得'exception'嗎? – Saifur 2015-01-12 16:17:06

1

你需要一個明確的等待。 See docs。示例代碼:

element = WebDriverWait(driver, 30).until(EC.visibility_of_element_located((By.XPATH,'//a[@href="/login/clear"]'))) 

然後只需單擊元素即可。

element.click() 

注意我說我的答案B/C - 在我的測試中,至少 - 你不必擔心italics標籤。這是文字,而不是按鈕 - 而且你沒有點擊文字。所以找到By.XPATH並選擇一個獨特的屬性(即在這種情況下href非常可愛不是class attr),然後點擊你的元素。

編輯:

請嘗試這一行:

element = WebDriverWait(self.browser, 30).until(EC.visibility_of_element_located((By.XPATH,'//a[@class="btn" and @href="/login/clear"]'))) 
+0

謝謝你的迴應。當我嘗試你的方法時,我有一個超時問題。它實際上在30秒內找不到它出於某種原因,這應該是更長的時間,因爲當我手動點擊時,網頁在大約2秒鐘內加載完畢。我也將「驅動程序」更改爲「self.browser」,因爲這是我的變量,指向webdriver.FireFox() – AlphaFoxtrot 2015-01-12 15:55:11

+0

@AlphaFoxtrot所以它工作還是需要更多的幫助?今天早上我在我的電腦上,並樂意盡我所能。讓我知道! – 2015-01-12 16:42:59

+0

更多的幫助,將不勝感激!當我運行你的代碼時,我會得到一個超時問題。然而,我沒有得到我原來的錯誤,現在的錯誤是:「raise TimeoutException(message)TimeoutException:Message:」沒有消息,因爲我沒有任何消息,但在你給它的時間內找不到該元素。我在你的代碼中改變的唯一東西是WebDriverWait()中的「驅動程序」到「self.browser」 – AlphaFoxtrot 2015-01-12 17:03:10