python webdriver需要帮助来测试找到的元素的“type()”以显示用户反馈。

2024-09-29 01:25:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用Python编写使用selenium webdriver的测试。我想知道的是,当我将一个链接存储为一个变量并用type()进行检查时,为什么它在使用时返回false检验.isclass(). 相关代码:

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))

我得到了结果

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

然后我用

print(inspect.isclass(salesForceLink))

这导致False

我错过了什么?为什么返回false?在

编辑: 我最终需要一个if语句来检查变量salesForceLink是否是“something”。在

示例:

^{pr2}$

Tags: 代码divfalsechild链接typeseleniumrow
2条回答

根据我的编辑,这是我发现的一个解决办法。在

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

编辑:根据下面的第一条评论,我发现虽然这句话等同于真的,但实际上我只是做了双倍的工作。我的工作解决方案是使用自定义包装函数的两行代码。第二个验证链接后的文本。在

^{pr2}$

对于感兴趣的用户,自定义函数如下:

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")

以及

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"

print debug函数只接受bool并打印true或false。 所有看起来像G.error或G.debug的变量都是代理全局变量,它们存储在一个空类中,以便在我的许多测试中使用。在

我希望这有帮助。在

我是一个人。人类是一个物种。在

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

这并不意味着我是一个物种。在


salesForceLinkFirefoxWebElementFirefoxWebElement是一个类。在

^{pr2}$

这并不意味着salesForceLink是一个类。在

相关问题 更多 >