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?

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Alan
  • 13
  • 1
  • 5

2 Answers2

0

Selenium contains methods known as waits to assist with this specific situation. I would read up on the documentation of explicit waits. Also, there is another Stack Overflow question which may be useful to look at.

In addition, and slightly off topic, it is atypical to use Asserts in the way you have in the code shown. In general, you should not think of using Asserts to check for conditions, but rather a condition that must be true for the test to pass, otherwise the test stops and there is no reason to continue on.

Cheers.

Community
  • 1
  • 1
bagelmakers
  • 384
  • 2
  • 15
  • Thanks, I read a bit on the waits and the other question. What do you think is the best way for me to check for a text condition in this loop? I dont think I need it to wait for the condition, I want it to check each number as fast as it can until the page loads an error on the count. – Alan Feb 23 '15 at 22:27
  • Can i use expected arguments? selenium.webdriver.support.expected_conditions.text_to_be_present_in_element(locator, text_) Bases: object An expectation for checking if the given text is present in the specified element. locator, text. But how do i get it to print if this is present, or just continue the loop without printing if not present? – Alan Feb 23 '15 at 22:37
  • Or even this which i also cant get to work, of course becasue the driver find element doesnt return a True or False value ,if it did I would have my answer here. So how can I return a true from finding this text? error = driver.find_element_by_link_text("Select Error").text if (error == true): print 'Someone is %.7r' %count else: print 'I have %.7r' %count – Alan Feb 23 '15 at 22:56
0

Assuming that your "Select Error" message is actually a link_text, you could try using a 'try/except' statement.

try:
   WebDriverWait(driver, 3).until(
      expected_conditions.text_to_be_present_in_element((By.LINK_TEXT, "Select Error"), "Select Error")
   )
   print "Link Found..."
except NoSuchElementException:
   pass

This code tries to find the "Select Error" link text for 3 seconds, catches the NoSuchElementException error, and then continues with your code.

rwbyrd
  • 416
  • 5
  • 24