2017-11-10 273 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

相关问题