如何使用Selenium按类查找Kendo按钮。

huangapple go评论115阅读模式
英文:

How to locate kendo button by class using Selenium

问题

我正在尝试使用Selenium在Python中定位并点击一个按钮。网页是基于kendo的,以下是按钮的相关代码:

<div class="k-header k-grid-toolbar k-grid-top">
   <button class="k-button k-button-icontext k-grid-excel" type="button">

我已尝试使用以下Python代码:

driver.find_element_by_class_name("k-button k-button-icontext k-grid-excel").click()

它引发了一个异常:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".k-grid-excel"}

有人可以提供一个更好的方法来找到这个按钮吗?

英文:

I am trying to locate and click a button using Selenium in python. The webpage is kendo based and here is the relevant code for the button:

&lt;div class=&quot;k-header k-grid-toolbar k-grid-top&quot;&gt;
   &lt;button class=&quot;k-button k-button-icontext k-grid-excel&quot; type=&quot;button&quot;&gt;

I have tried using this python code:

driver.find_element_by_class_name(&quot;k-button k-button-icontext k-grid-excel&quot;).click()

It raises an exception:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {&quot;method&quot;:&quot;css selector&quot;,&quot;selector&quot;:&quot;.k-grid-excel&quot;}

Can anyone advise on a better method to find the button?

答案1

得分: 0

我最终使用CSS选择器来查找它,并且我还不得不在其中添加等待:

wait = WebDriverWait(driver, 15)

element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, '#grid > div.k-header.k-grid-toolbar.k-grid-top > button')))
element.click()
英文:

I ended up using the CSS Selector to find it and I had to put a wait in there as well:

wait = WebDriverWait(driver, 15)

element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, &#39;#grid &gt; div.k-header.k-grid-toolbar.k-grid-top &#39;
                                                                      &#39;&gt; button&#39;)))
element.click()

</details>



huangapple
  • 本文由 发表于 2020年1月7日 00:37:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/59615777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定