2016-05-13 20 views
0

「ListFirst」という名前のiframeからすべてのアンカータグテキストを取得します。私はテキストを繰り返して、私はここで私が[e '' text '] == u'AGENT-WIN3E64'とした比較をclick.Butしたい文字列 'AGENT-WIN3E64'と比較しようとしています同じです。助けてください。私はあなたが記述状況を再作成しようとしたの下ユニコードの要素リストと文字列を比較するpython splinter

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 ']") 
関連する問題