Python 3.x 无法识别selenium测试中单击的按钮

Python 3.x 无法识别selenium测试中单击的按钮,python-3.x,selenium,xpath,css-selectors,webdriverwait,Python 3.x,Selenium,Xpath,Css Selectors,Webdriverwait,我有这个按钮 <button _ngcontent-gyx-c3="" class="btn btn-gradient-primary btn-icon-text p-3 col-12" routerlink="/certificate-request/create" routerlinkactive="active" type="button" tabindex="0"><i _ngcontent-gyx-c3="" class="icon-plus btn-icon-pre

我有这个按钮

<button _ngcontent-gyx-c3="" class="btn btn-gradient-primary btn-icon-text p-3 col-12" routerlink="/certificate-request/create" routerlinkactive="active" type="button" tabindex="0"><i _ngcontent-gyx-c3="" class="icon-plus btn-icon-prepend"></i> New request </button>
但我有以下例外:

没有这样的元素:无法定位元素:{method:xpath,selector://button[@class=btn btn梯度主btn图标文本p-3列-12][.=新请求]} 会话信息:无头镀铬=72.0.3626.109


你可以选择在你的按钮上加一个id,然后像这样做吗

driver.find_element_by_id(<id_of_button>)

有多种xpath模式可以工作,这与您的问题中的模式非常接近

driver.find_element_by_xpath('//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][text(),"New request"]')
所需的元素是一个元素,因此要使用鼠标单击该元素,必须使元素可单击,并且可以使用以下任一选项:

使用CSS_选择器:

使用XPATH:

注意:您必须添加以下导入:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

我没有访问前端代码的权限,我只是测试它,所以我无法更改源代码中的任何内容。但是,看起来按钮以某种方式嵌套在一起。我已经编辑了我的答案。
driver.find_element_by_xpath('//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][text(),"New request"]')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-gradient-primary.btn-icon-text[routerlink='/certificate-request/create'][routerlinkactive='active']>i.icon-plus.btn-icon-prepend"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@routerlink='/certificate-request/create' and @routerlinkactive='active']/i[@class='icon-plus btn-icon-prepend']"))).click()
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC