我有这个按钮
<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>
我如何选择进行seinuim测试 我尝试了这个:
driver.find_element_by_xpath('//button[@class="btn btn-gradient-primary btn-icon-text p-3 col-12"][.="New request"]').click()
但是我有以下例外情况:
没有这样的元素:无法找到元素:{“ method”:“ xpath”,“ selector”:“ // button [@ class =” btn btn-gradient-primary btn-icon-text p-3 col- 12“] [。=”新请求“]”} (会话信息:headless chrome = 72.0.3626.109)
答案 0 :(得分:0)
可以使用多个xpath模式,这与您所提出的问题很接近。
disconnectedCallback
答案 1 :(得分:0)
所需元素是Angular元素,因此要使用Selenium单击该元素,您必须为element_to_be_clickable()
引入WebDriverWait,并且可以使用以下任意一种方法Locator Strategies:
使用CSS_SELECTOR
:
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()
使用XPATH
:
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
答案 2 :(得分:-1)
您可以选择在按钮上放置一个id并这样做吗?
driver.find_element_by_id(<id_of_button>)
编辑:
怎么样?
driver.find_element_by_xpath('//button[@class="icon-plus btn-icon-prepend"][.="New request"]').click()