-2

I'm writing test using selenium webdriver using Python. What i would like to know is when I store a link as a variable and do a check on it with type() why does it return false when using inspect.isclass(). relevant code:

salesForceLink = findCss(sel, "div.row:nth-child(1) > div:nth-child(1) > 
    div:nth-child(1) > div:nth-child(3) > a:nth-child(1)")

print(type(salesForceLink))

i get the results,

<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>

Then I check against it with,

print(inspect.isclass(salesForceLink))

this results in False

what am I missing? why is it returning false?

EDIT: I ultimately need an if statement to check the whether the variable salesForceLink is a "something".

Example:

if "salesForceLink is something" == True
    print("Sales Force link found")
else:
    print("Sales force link not found")
modnarrandom
  • 142
  • 1
  • 13
  • 1
    Classes *are types*. The `type` function is telling you that `salesForceLink` is an *instance of* `` – juanpa.arrivillaga Sep 07 '18 at 16:20
  • So, consider `type('foo')` and `type(string)`... – juanpa.arrivillaga Sep 07 '18 at 16:21
  • I think i follow, so this `print(inspect.isclass(salesForceLink)) is false because its not a class it's self, but the class object. – modnarrandom Sep 07 '18 at 16:31
  • OK, but use the terminology correctly to avoid confusion: it is *an instance*. Classes are objects too, they are class objects, and classes are instances of type `type`. That's right, `type` is a `type` as well. It is a metaclass. – juanpa.arrivillaga Sep 07 '18 at 16:33
  • What does `findCss()` return? WebElement in case link found and what about case when not found? – Andersson Sep 07 '18 at 16:59
  • 1
    This is definitely an X-Y problem. `if salesForceLink: ... else: ... ` or `try: salesForceLink except: ...` should be enough – Andersson Sep 07 '18 at 17:17
  • The confusion here is possibly between Python classes as in templates for objects, and HTML/CSS classes which are just a type of markup. When doing web stuff in Python, the context and terminology need to stay very clear. – Mad Physicist Sep 07 '18 at 23:16
  • @Andersson its a custom wrapped function less typing than stock way to find element by css. also it returns, `` when I check the type – modnarrandom Sep 08 '18 at 15:48
  • @modnarrandom , WebElement is what built in `find_element` method returns in case element found. I asked *what does it return when element not found?* – Andersson Sep 08 '18 at 15:59
  • why the down votes? i changed the question to better reflect the issue i was having. – modnarrandom Sep 08 '18 at 15:59
  • Because this is the basics of Python: `` doesn't mean that the output of `type()` is the *class*. It mean *instance of 'ClassName' class*. Also checking element existence is also basics of Selenium... – Andersson Sep 08 '18 at 16:03
  • Possible duplicate of [Check if element exists python selenium](https://stackoverflow.com/questions/45695874/check-if-element-exists-python-selenium) – Andersson Sep 08 '18 at 16:04
  • yes checking elements in selenium yes, but having never encountered checking the "type" of an object before i doubt that its "basic" knowledge. I've done some python training and checking/ verifying an instance type was never used. to me it seems more of an advanced debugging method. oh well. – modnarrandom Sep 08 '18 at 17:26
  • Also its not a duplicate of that other question, I ultimately needed to verify the verification to output to the console for readability/ understanding for other users. Just running the code isn't enough to someone just running the test. – modnarrandom Sep 08 '18 at 17:29

2 Answers2

0

I am a human. Human is a species.

>>> species(me)
<species 'homo.sapiens.sapiens'>

That doesn't mean I am a species.


salesForceLink is a FirefoxWebElement. FirefoxWebElement is a class.

>>> type(salesForceLink)
<class 'selenium.webdriver.firefox.webelement.FirefoxWebElement'>

That doesn't mean salesForceLink is a class.

user2357112
  • 260,549
  • 28
  • 431
  • 505
-1

As per my Edit this is what I found to be a solution.

if isinstance(salesForceLink, object) == True:
        print("sales Force link found")
    else:
        print("sales force link not found")

Edit: Per the first comment below I had found that while the statement equates to true, that really I was just doing double the work. my working solution is just two line using custom wrapped functions. the second one verifies the text after following the link.

findLink(sel, "div.row:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3) > a:nth-child(1)", "sales force link")
verifyText(sel, ".freebirdFormviewerViewHeaderTitle" , "Salesforce Feedback")

The custom functions are as follows for anyone interested:

def findLink(browser, css, label="", type="link:", x=1):
if x == 0:
    if findCss(browser, css, type="link:"):
        link = findCss(browser, css)
        label  = element.text
        type = element.get_attribute('type')
        printDebug(type + ": \"" + label + "\" found")
        return link
    else:
        G.errors += 1
        G.log[G.errors] = G.page + "ERROR" + label + " not found"
        #print("ERROR" + label + " not found")
elif x == 1:
    if findCss(browser, css):
        element = findCss(browser, css)
        label  = element.text
        type = element.get_attribute('type')
        element.click()
        printDebug(type + ": \"" + label + "\" found")
    else:
        G.errors += 1
        G.log[G.errors] = G.page + "ERROR" + label + " not found"

else:
    printDebug("Last flag must be 0 or 1, 0 is default")

and

def verifyText(browser, css, textToCheck=""):
    if findCss(browser, css):
        element = findCss(browser, css)
        elementText = element.text
    else:
        G.errors += 1
        G.log[G.errors] = G.page + "ERROR text: Could not find element"

    if textToCheck == "" and elementText != textToCheck:
        printDebug('text: "' + elementText + '" element Not Empty ' )
        return str(elementText)

    elif textToCheck == "" and elementText == textToCheck:
        printDebug("text: Element Empty")    

    elif elementText == textToCheck:
        printDebug('text: "' + elementText + '" found')
        return str(elementText)

    elif elementText != textToCheck:
        G.errors += 1
        G.log[G.errors] = G.page + ' ERROR text: "' + textToCheck + '" does not match "' + elementText + '"'

    else:
        G.errors += 1
        G.log[G.errors] = G.page + "ERROR text: Unexpected Text Search Error"

The print debug function just takes takes in a bool and prints if true or false. All of the variables that look like G.error, or G.debug are proxy global variables that are stored in an empty class for use throughout the many test i have.

I hope that helps.

modnarrandom
  • 142
  • 1
  • 13