2017-08-25 10 views
0

Google Scholarのページから引用を取得するために、このコードをいくつかクリックしてシミュレーションしました。forループ内でセレン(find_by_id)メソッドが機能しない

def APAcite (papers): 
    gscholar= 'https://scholar.google.com/scholar?hl=en&q=' 
    cites = [] 
    for p in papers: 
     print(p) 
     address = gscholar + p 
     Nbrowser = webdriver.Chrome(chrome_p) 
     Nbrowser.get(address) 
     Nbrowser.find_element_by_link_text('Cite').click() 
     APA = Nbrowser.find_element_by_id('gs_cit1').click() 
     Bib = Nbrowser.find_element_by_link_text('BibTeX').click() 
     cit_pg = browser.find_element_by_css_selector('''body > pre''') 
     cites.append(cit_pg.text) 
     Nbrowser.close() 
    return cites 

は、その後、私は論文

l = ['Sustainability and the measurement of wealth: further reflections'] 

のリストを定義したが、私はそれを実行したときに、私は次のエラーを取得する:

<ipython-input-309-4208af266f79> in APAcite(papers) 
     8   Nbrowser.get(address) 
     9   Nbrowser.find_element_by_link_text('Cite').click() 
---> 10   APA = Nbrowser.find_element_by_id('gs_cit1').click() 
    11   Bib = Nbrowser.find_element_by_link_text('BibTeX').click() 
    12   cit_pg = browser.find_element_by_css_selector('''body > pre''') 

。 。 。

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"id","selector":"gs_cit1"} 

私がページを検査し、このIDは、もう一つのポイント id for selected element

を存在することを確認してもらうが、私は関数の外とforループせずにコードを実行したときに、私はすべてのエラーを取得しないとそれはうまく動作します!

助けてもらえますか?

答えて

0

ただ、基本的にはあなたがあまりにも速く動いている

Nbrowser.get(address) 

後コード

Nbrowser.implicitly_wait(20) 

の線の下に追加します。クリックした後、それが表示されるように、テキストのためのいくつかの時間がかかるし、あなたがそこにいないのid探してみてください(まだ!)を

私はコードに

Sustainability and the measurement of wealth: further reflections ['@article{arrow2013sustainability,\n title={Sustainability and the measurement of wealth: further reflections},\n author={Arrow, Kenneth J and Dasgupta, Partha and Goulder, Lawrence H and Mumford, Kevin J and Oleson, Kirsten},\n journal={Environment and Development Economics},\n volume={18},\n number={4},\n pages={504--516},\n year={2013},\n publisher={Cambridge University Press}\n}']

+0

おかげで多くのことを固定した後、出力の下に取得。..チャームのように働いた! –

+0

暗黙の待機を使用しないでください。それは悪い習慣です。代わりに明示的な待機を追加します。その要素がクリック可能になるのを待ってからクリックしてください。 – JeffC

関連する問題