python splinter comparing unicode elementlist with string

时间:2016-05-13 15:33:08

标签: python-unicode splinter

I want to get all the anchor tag text from an iframe named "ListFirst". I'm trying to iterate text and comparing each with the string 'AGENT-WIN3E64 ' that I want to click.But the comparison I made here e['text'] == u'AGENT-WIN3E64 ' becomes false event though the strings are same. Please help.

Here is my code:

  with iframe12.get_iframe('ListFirst') as iframe1231:
        anchorList=iframe1231.find_by_tag('a')   
        for e in anchorList:
            if e['text'] == u'AGENT-WIN3E64 ':   #unicode string comparison
                e.click()
                break;

1 个答案:

答案 0 :(得分:0)

通过下面的设置,我尝试重新创建您描述的情况。下面的.py脚本似乎发现锚点很好。

的index.html,

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <iframe name="mainframe" src="iframe1.html"></iframe>
    </body>
</html>

iframe1.html,

<html>
        <head></head>
        <body>
                <iframe name="childframe" src="iframe2.html"></frame>
        </body>
</html>

iframe2.html,

<html>
        <head></head>
        <body>
                <a href="/a">AGENT-WIN3E64 </a>
                <a href="/b">b</a>
                <a href="/c">c</a>
                <a href="/d">d</a>
                <a href="/e">e</a>
        </body>
</html>

test.py

from splinter import Browser

browser = Browser('firefox', wait_time=10)
browser.visit("http://localhost:8000/index.html")

# get mainframe
with browser.get_iframe('mainframe') as mainframe:

    # get childframe
    with mainframe.get_iframe('childframe') as childframe:

        anchorList = childframe.find_by_tag('a')
        for e in anchorList:
            if e['text'] == u'AGENT-WIN3E64 ':   #unicode string comparison
                print "found anchor"
                e.click()
                break;

此输出,

found anchor

但请注意,您也可以使用xpath

直接找到锚点
anchor = childframe.find_by_xpath("//a[text() = 'AGENT-WIN3E64 ']")