2016-05-13 57 views
0

我想從名爲「ListFirst」的iframe中獲取所有的定位標記文本。我試圖迭代文本,並將每個字符串與我想單擊的字符串「AGENT-WIN3E64」進行比較。但是,我在此處做出的比較e ['text'] == u'AGENT-WIN3E64'通過字符串變爲虛假事件是一樣的。請幫忙。python splinter比較unicode元素列表和字符串

這裏是我的代碼:

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; 

回答

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