问题
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