Python Selenium单击带有类的按钮

时间:2019-12-22 11:57:53

标签: python selenium selenium-webdriver css-selectors selenium-chromedriver

我是python硒的新手,我试图单击具有以下HTML结构的按钮:

queue

我已经尝试过点击提交按钮。

<div class="wpsafe-top text-center"> <h1><strong><span style="color: #ce2525;">Click on Submit to Get Link</span></strong></h1><br> <form action="https://url.com/post" method="post"> <input type="hidden" name="getlink" value="NVudM3E"> <input type="hidden" name="newwpsafelink" value="eyJsaW5rIjoiTlZ1ZE0zRSIsImFkczEiOiIiLCJhZHMyIjoiIiwibG9nbyI6IiIsImltYWdlMSI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvZ2VuLnBuZyIsImltYWdlMiI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvcGx6LnBuZyIsImltYWdlMyI6Imh0dHA6XC9cL3RlY2hjb2RlY3MuY29tXC93cC1jb250ZW50XC91cGxvYWRzXC8yMDE5XC8xMlwvY2xrLnBuZyIsImxpbmtyIjoiaHR0cHM6XC9cL3RlY2hjb2RlY3MuY29tP3NhZmVsaW5rX3JlZGlyZWN0PTUlNUIlOUQzcSIsImRlbGF5dGV4dCI6IjxiPjxoMiBzdHlsZT0nY29sb3I6cmVkJz5Mb2FkaW5nIExpbmsgLi4uIFdhaXQgPHNwYW4gaWQ9XCJ3cHNhZmUtdGltZVwiPjEyPFwvc3Bhbj4gU2Vjb25kczxcL2gyPjxcL2I+IiwiZGVsYXkiOiIxMiIsImFkYiI6IjIiLCJhZGIxIjoiIiwiYWRiMiI6IiJ9"> <input class="btn btn-primary" type="submit" value="Submit"> </form> </div>

但是我总是以错误结尾

driver.find_element_by_css_selector('btn btn-primary').click()

1 个答案:

答案 0 :(得分:1)

find_element_by_css_selector('btn btn-primary')将看起来是具有标签btn-primary的元素,而其祖先元素具有标签btn

您需要告诉driver这是带有.的类

driver.find_element_by_css_selector('.btn.btn-primary').click()

或明确地

driver.find_element_by_css_selector('[class="btn btn-primary"]').click()