2

I am trying to interact with a sign out button but I just can't seem to click on it using link text or xpath.

I have tried following these answers with no luck:

Here is my code:

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")

The first line under test_logout calls a function that waits for a certain element to appear on the webpage so I can see that the page has been loaded. Then I try to click the sign out button.

This is the HTML(element is at class="btn" href="/login/clear"):

<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>

When I try finding by link_text, the element can't be found. Running this code gives me a stacktrace error saying:

ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with

Edit: I've tried Saifur's answer and updated my code to his answer but now I get: AttributeError: 'BasicTestCase' object has no attribute 'find_element'. I've tried changing "self" to "self.browser" as an argument in WebDriverWait() but I would then get my original error.

2 Answers 2

1

Use explicit wait and relativexpath

like //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")
Sign up to request clarification or add additional context in comments.

11 Comments

Thank you for your answer. I get an error running your code though. AttributeError: 'BasicTestCase' object has no attribute 'find_element'. I tried changing the "self" in WebDriverWait to "self.browser" but I would get my original error. Also I believe you were missing an extra parenthesis.
Thanks for bringing that to my attention.I corrected the ). And, you should be passing the driver instance inside ` WebDriverWait(self, 10)` whatever it is and please make sure you have imported the correct references as well. See this
Are you still getting the exception?
I don't get the exception. Just that Attribute error when I run your code. There are some error that pop up in the Traceback from the selenium expected_conditions pakcage.
It did not. The test stopped and failed because of that Attribute error. I'm using nosetests by the way if that helps.
|
1

You need an explicit wait. See docs. Example code:

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

And then just click the element.

element.click()

Note I added my answer b/c -- in my tests at least -- you don't need to worry about the italics tag. That's for the text, not the button -- and you're not clicking the text. So find By.XPATH and select a unique attribute (i.e., the href in this case and very likley not the class attr), and then click your element.

EDIT:

please try this line:

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

12 Comments

Thank you for your response. When I tried your method I got a timeout issue. It actually couldn't find it for some reason within 30 seconds which should be more then enough time since the webpage loads within about 2 seconds when I'm clicking through manually. I also changed "driver" to "self.browser" since that is my variable that points to webdriver.FireFox()
@AlphaFoxtrot so is it working or do you need a bit more assistance? I'm at my computer this morning and would happily do what I can. let me know!
More assistance would be appreciated! When I run your code I would get a timeout issue. I don't get my original error however, the error now is: "raise TimeoutException(message) TimeoutException: Message: " There is no message since I have none but couldn't find the element within the time you gave it. The only thing I changed in your code was "driver" in WebDriverWait() to "self.browser"
I've updated my code according to Saifur's answer and I'm working with him as well to try and solve this problem. I get a different error with his code than yours.
@AlphaFoxtrot okay, try his/her stuff and then, if it doesn't work, let me know. I'm happy to help, but I'm sure you and Saifur can get it. Let me know! Thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.