python - How to verify dynamic element present in Selenium WebDriver -
i'm writing scripts in python using selenium webdriver test web application.
the web application allows users ask questions, added div asked. each question div has own "upvote" , "downvote" link/image. image changes when "upvote" icon clicked, active inactive, or vice versa. know xpath upvote icon is:
"//div[@id='recent-questions-container']/div/div/div/div/div[2]/div/ul/li/a/i"
this "i" @ end of path class, , either
<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 clicked. want verify correct image in place upon being clicked. how can that? ideally, i'd perform verification using assertions, la:
self.asserttrue(self.is_element_present( ... ))
here html 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>
you can use get_attribute class
attribute , search if class contains clicked make sure in fact clicked
#make sure selector 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 click element , should expecting change class active. find count should more 0
driver = self.driver #perform click here #the idea avoid nosuchelement exception # , see if element count greater 0 assert (len(driver.find_elements_by_css_selector(".student-view.thumbs-up-to-active")) > 0)
Comments
Post a Comment