英文:
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:
<div class="k-header k-grid-toolbar k-grid-top">
<button class="k-button k-button-icontext k-grid-excel" type="button">
I have tried using this python code:
driver.find_element_by_class_name("k-button k-button-icontext k-grid-excel").click()
It raises an exception:
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".k-grid-excel"}
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, '#grid > div.k-header.k-grid-toolbar.k-grid-top '
'> button')))
element.click()
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论