Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 97778

Learning to use Assert and Asserttrue in Python for selenium web driver

$
0
0

Im trying to make a python webdriver to load a webpage and then assert true and run a print command if a text or object is there, and if not I want it to just continue running my loop. Im a noob to python and have been learning from Learn python the Hard Way, and reading documentation. Ive spent the last day or so trying to get my code finding text or elements, but it doesnt feed back info...Here is my code so far minus the top part about going to the webpage, I am just stuff on this count loop assert logic.

count = 1000.00
while count < 1000.03:
    driver.find_element_by_id("select").clear()
    driver.find_element_by_id("select").send_keys(str(count))
    time.sleep(1)
    driver.find_element_by_id("BP").click()
    driver.find_element_by_id("BP").click()
    count += 0.01 ## increases count to test
    highervalue = driver.find_element_by_link_text("Select Error")
    assertTrue(driver.link_contains_match_for("")) ##could also be, ##text_containt_match_for("ex") or driver.assertTrue(element in WEBPAGE)??
        print 'Someone is %.7r' %count
    else:
        print 'I have %.7r' %count
    time.sleep(1)

then the loop starts over again. The issue i am having is I want to find "Select Error" on the webpage in some kind of form, link, or text, and then if it is there print me a msg, and if not, to just continue my loop.

Is it better to use assert/asserttrue, or something like

def is_element_present(self, how, what):
    try: self.driver.find_element(by=how, value=what)
    except NoSuchElementException, e: return False
    return True

or

Some other examples I have searched about that could be used:

self.assertTrue(self.is_element_present(By.ID, "FOO"))

self.assertTrue(self.is_element_present(By.TEXT, "BAR"))

self.assertTrue(self.is_text_present("FOO"))

self.assertTrue(self.driver.is_text_present("FOO"))

Can someone let me know how I would write the part when I find something in the webpage and it gives me feedback if found?


Viewing all articles
Browse latest Browse all 97778

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>