问题
I am working on a selenium script in Python, where I am on this stage trying to locate a submit button.
HTML
<div class="submit-buttons">
<button class="submit" type="submit">Filter</button>
</div>
I've tried and this has not worked.
So I am out of solutions:
browser.find_element_by_partial_link_text('Filter').click()
browser.find_element_by_link_text('Filter').click()
browser.find_element_by_class_name('submit').click()
回答1:
Try xpath solution:
driver.find_element_by_xpath('//div[@class="submit-buttons"]/button[@class="submit"]')
If it is still not identifying, the element might be inside a frame, and you have to switch to that frame before finding the element.
回答2:
By link text or by partial link text locators are going to work with links only - a
elements. Here you have a button. If you want to use the button text, use the following "by xpath" locator:
//button[. = "Filter"]
回答3:
This code should work :
driver.find_element_by_xpath('//button[text()='Filter']')
回答4:
driver.findElement(By.cssSelector("button[type=\"submit\"]")).click();
来源:https://stackoverflow.com/questions/34547370/selenium-locate-submit-button