2016-03-28 9 views
2

セレンがView All Companiesボタンをクリックしようとしていますが、何が間違っているのか分かりません。それはno element foundセレンを見つけてそれをクリックしてください

htmlコード

<div class="screener-toggles"> 
    <div class="buttons"> 
    <span class="button selected" data-name="advanced-screener">Search by Screener<span data-name="advanced-screener" class="arrow selected"></span></span> 
    <span class="button" data-name="alpha-factors">Search by Alpha Factors<span data-name="alpha-factors" class="arrow"></span></span> 
    <span class="button" data-name="all-companies">View All Companies<span data-name="all-companies" class="arrow"></span></span> 
    </div> 
</div> 

Pythonのコードは私がfind_elements_by_class_nameを使用すべきではない

element1 = driver.find_elements_by_class_name('View All Companies') 
element1.click() 
# I have tried all-companies instead of View All Companies as well. But still doesn't work 

を書いて返しますか?

私が間違っていることについてアドバイスをいただければ幸いです!

答えて

1

View All Companiesテキストではなく、クラスである:あなたのHTMLに見て、慎重に

のクラス名、または他のロケータを選択してください、それは

+0

ありがとう@Guy! 'css_selector'も考慮したはずです.. –

1

はい、find_element_by_class_nameの代わりにfind_elements_by_class_nameを使用しないでください。

find_elements_by_class_nameは、ロケータが1つ以上の要素を返すと予想されるときに使用されます。特定の要素の場合はfind_element_by_class_nameのみを使用してください。

あなたのHTMLコードには、クラス名が表示されません。View All Companiesです。 "//span[contains(text(),'View All Companies')]"

+0

が含まれていdata-name属性によってテキストで探してみてください投稿したHTMLは? –

+0

はい、あなたは..あなたが探している正確な要素/ HTMLコードにコメントしてください。 –

2

トライのxpathはあなたを助けることを願っています。 ...に基づいたボタンをクリックする別の方法はありcss_selectorまたはxpath

element1 = find_element_by_css_selector('span:contains("View All Companies")') 

element1 = find_element_by_xpath('//span[contains(text(), "View All Companies")]') 

またはボタンに関わる全てのHTMLたall-companies

element1 = find_element_by_css_selector('span[data-name*="all-companies"]') 
+1

Thanks @ Leon。これは素晴らしい作品です! –

関連する問題