How to click on a button webelement using css_selector with Selenium webdriver for identical button types

血红的双手。 提交于 2021-02-10 22:26:55

问题


For a HTML,

<div class="some-class">
    <button type="button">Ok</button>
    <button type="button">Cancel</button>

Both button type are the same for two different buttons so how can I click on the Ok button using find_element_by_css_selector? I tried driver.find_element_by_css_selector("div.some-class > button.button[1]").click() but it didn't work for me.


回答1:


Use div.some-class > button.button as selector becasue find_element_by_css_selector return the first matched WebElement:

driver.find_element_by_css_selector("div.some-class > button").click()

If you want be explicit use :nth-child(1) or :first-child.

UPDATE

selector was wrong. Selector should be div.some-class > button[type=button] or simply div.some-class > button.



来源:https://stackoverflow.com/questions/18744094/how-to-click-on-a-button-webelement-using-css-selector-with-selenium-webdriver-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!