Tanner
Tanner

Reputation: 103

How to verify dynamic element present in Selenium WebDriver

I'm writing scripts in Python using Selenium WebDriver to test a web application.

The web application allows users to ask questions, which are added to a div as they are asked. Each question div has their own "upvote" and "downvote" link/image. The image changes when the "upvote" icon is clicked, from active to inactive, or vice versa. I know that the xpath to this upvote icon is:

"//div[@id='recent-questions-container']/div/div/div/div/div[2]/div/ul/li/a/i"

This "i" at the end of the path is a class, and will either be

<i class="fa fa-2x fa-thumbs-o-up"></i>

or

<i class="fa fa-2x fa-thumbs-up clicked"></i>

depending on whether or not it is clicked. I want to verify that the correct image is in place upon being clicked. How can I do that? Ideally, I'd like to perform this verification using assertions, a la:

self.assertTrue(self.is_element_present( ... ))

Here is the html for what I'm talking about

<div id="recent-questions-container">
    <div question_id="randomly generated blah" class="q row recent-question animated pulse" data-score="0">
        <div class="col-xs-12">
        <div class="question-content">
            <p>3</p>


            <div class="row question-controls">
                <div class="owner-view hidden">
                    ...
                <div class="student-view">
                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 question-controls-left">
                        <ul class="list-inline">
                            <li>
                                <a href="#" class="student-view thumbs-up-to-active">
                                    <i class="fa fa-thumbs-o-up fa-2x"></i></a>
                            </li>
                            <li>
                                <span class="num-votes">0</span>
                            </li>
                            <li class="thumbs-down-li">
                                <a href="#" class="student-view thumbs-down-to-active">
                                    <i class="fa fa-thumbs-o-down fa-2x"></i></a>
                            </li>
                        </ul>
                    </div>

                </div>

            </div>
            <hr>
        </div>
    </div>
    </div>
... other questions ... 
            </div>
            <hr>
        </div>
    </div>
    </div>


            </div>

Upvotes: 1

Views: 1886

Answers (1)

Saifur
Saifur

Reputation: 16201

You can use get_attribute to get the class attribute and then do a search if the class contains clicked to make sure it was in fact clicked

#make sure the selector is correct
xpath = "//div[@id='recent-questions-container']/div/div/div/div/div[2]/div/ul/li/a/i"
element = driver.find_element(By.XPATH, xpath)
attr = element.get_attribute('class')
if  'clicked' in attr:
    print("clicked")
else:
    print("was not clicked")

Edit

I would click the element and now should be expecting to change the class to active. Then find the count which should be more than 0

driver = self.driver
#perform click here
#the idea is to avoid the NoSuchElement exception
# and see if the element count is greater than 0
assert (len(driver.find_elements_by_css_selector(".student-view.thumbs-up-to-active")) > 0)

Upvotes: 1

Related Questions