0

I am using Python for Selenium RC and I am using the following selenium command:

self.se.get_attribute("CSS")

This command asserts when the respective CSS mentioned is not found on the webpage.

Is it possible for me to avoid the assert from happening and instead of the assert, another command is executed (On the condition that the assert has taken place)

self.se.get_text("another CSS")

Is there a way in python to capture the assert and just store its value and continue forward without terminating the program? Could someone please help me with this? Thanks

Sunny
  • 7,444
  • 22
  • 63
  • 104

2 Answers2

0

Assign the value of the method checking if selector is present to a boolean value. Ruby code would be similar to :

selector_present? = @driver.find_elements(:css, "div#id_name").length > 0
#some other code in your test script
overall_result = selector_present? && other_selector_present?
overall_result.should be_true

Python syntax would be similar.

Nikita Barsukov
  • 2,957
  • 3
  • 32
  • 39
  • so you mean i check if the value returned by that line of code is greater than 0. In case it is less than 0, does it mean that an assertion or error has taken place? – Sunny Oct 19 '11 at 09:18
  • Yes, check it and assign the result true or false to a variable (selector_present? in my case). there's no assertion, so you can use it with other variables for assertion at the end of your script (in my code assertion is in the last line). – Nikita Barsukov Oct 19 '11 at 09:33
  • but the problem is somehow se.is_element_present() always returns True, even when I cannot see that tag. – Sunny Oct 19 '11 at 09:36
  • I am not able to follow how to trap the exception as a boolean. could you please explain a little more in detail. – Sunny Oct 19 '11 at 10:03
0

If when 'CSS' is not valid, selenium raises a TypeError, you can catch and handle the exception in a try..except block:

try:
    self.se.get_attribute("CSS")
except TypeError:
    self.se.get_text("another CSS")

This is an example of the "Easier to ask forgiveness (EAFP) than permission" coding style. The alternative is the Look before you leap (LBYL) coding style (see Nikita Barsukov answer for example). Both are possible, though I think EAFP is preferred in Python because doesn't clutter the code with conditionals which may or may not fully capture the exceptional condition. EAFP is also fast when the exception is not raised too often. If the exception is raised often, LBYL may be preferrable.

Community
  • 1
  • 1
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
  • C:\Python27\lib\site-packages\selenium-2.8.1-py2.7.egg\selenium\selenium.py:224: TypeError: 'NoneType' object is not subscriptable – Sunny Oct 19 '11 at 09:29
  • This is the exact exception thrown. SO how would the above code change? thanks – Sunny Oct 19 '11 at 09:29
  • In that case, change `except Exception` to `except TypeError`. – unutbu Oct 19 '11 at 09:33
  • Would it now look like this: try: self.se.get_attribute("CSS") except TypeError: 'NoneType' object is not subscriptable: self.se.get_text("another CSS") – Sunny Oct 19 '11 at 09:33
  • I'm assuming `self.se` is not None since in another comment you say `se.is_element_present()` is `True`. So, if `self.se.get_text()` is also raising a `TypeError`, then you can catch and handle that exception with another (nested) `try..except` block (or do nothing and let the program halt on that exception). – unutbu Oct 19 '11 at 09:50
  • That was basically because of the following reason: - I have a attribute 'title' in my HTML tag while sometimes visible and sometimes not visible on the webpage (based on certain underlying code) – Sunny Oct 19 '11 at 10:00
  • - Initially I had thought that I would first check whether that attribute exists using self.se.is_element_present() and then based on the result of this, I would either get the attribute value (self.se.get_attribute()) if True and other just text HTML tag text of False (self.se.get_text()) - But I found that even when the title attribute is not showing in the HTML, self.se.is_element_present() is returning True and hence my results got skewed. - And the other problem was that doing self.se.get_attribute() leads to an exception when the title attribute (or any for that matter) is not found. – Sunny Oct 19 '11 at 10:00
  • - So i decided to figure out a way of using this exception as my if/else condition. - Please let me know if this sounds reasonable or would you recomend something else. – Sunny Oct 19 '11 at 10:01
  • everything didnt fit in one comment do i needed three comments – Sunny Oct 19 '11 at 10:01
  • Yes, this sounds fine. The EAFP coding style encourages you to just try (first) and handle exceptions (as appropriate), instead of using the LBYL if-else block. – unutbu Oct 19 '11 at 10:16