2017-11-10 275 views
-1

我試圖用Chrome webdriver點擊元素,但我無法找出點擊它的方法。
該頁面顯示我在登錄後,所以我不能發佈的網站URL
元素的HTML代碼是:找不到點擊Chrome webdriver中元素的方法 - Selenium

<gf-dashboard-card class="ng-scope ng-isolate-scope" href="/chat"> 
    <div class="card"> 
     <div class="card-icon"> 
      <i class="card-icon material-icons ng- 
      binding">question_answer</i> 
     </div> 
     <span style="text- 
     overflow:ellipsis;white-space:nowrap;overflow:hidden" 
     class="md-subhead ng-binding">Conversations</span> 
    </div> 
</gf-dashboard-card> 

我試過任何方式,我不能想點擊的元素但我總是不斷收到錯誤:

Message: no such element: Unable to locate element 

元素的XPath是

//*[@id="dashboard-cards"]/gf-dashboard-card[1] 

哪給了我什麼。

一個有趣的事情是,在Firefox的webdriver我可以

driver.find_element_by_class_name("ng-isolate-scope").click() 

點擊它,但它是無法與Chrome工作。
我可以

$('.md-subhead').click(); 

通過控制檯點擊元素,但還沒有想出如何通過Selenium運行此。
任何想法如何我可以運行它?

+0

嘗試'driver.find_element_by_css_selector(「span.md-小標題」)' – Mangohero1

+0

調試代碼時,看到問題是造成前點擊添加一個長時間的睡眠頁面未完成加載。 – yong

回答

0

這是一個加載和等待時間的問題。
添加:

time.sleep(10) 

然後:

driver.find_element_by_xpath("//*[@id='dashboard-cards']/gf-dashboard- 
card[1]").click() 

它運作良好

+0

Selenium具有輪詢這類事情的功能。請參閱http://selenium-python.readthedocs.io/waits.html。 –

+0

@DavidKnipe正是我所需要的,謝謝! – Kraki

+0

但睡眠()不是更好的解決方案 – iamsankalp89

1

你可能想使用的implicitly_wait代替time.sleep等待元素變成可點擊的。當在10秒超時之前發現該元素時,它將繼續而不是等待剩餘時間。

從文檔:

An implicit wait tells WebDriver to poll the DOM for a certain amount of time when trying to find any element (or elements) not immediately available. The default setting is 0. Once set, the implicit wait is set for the life of the WebDriver object.

from selenium import webdriver 

driver = webdriver.Firefox() 
driver.implicitly_wait(10) # seconds 
driver.find_element_by_xpath("//*[@id='dashboard-cards']/gf-dashboard-card[1]").click() 
+0

謝謝!使用它比使用time.sleep更有意義 – Kraki

相關問題