我正在尝试使用Selenium自动执行工作中的一项任务。一切都很顺利,直到我不得不点击过滤器按钮。
我试过使用find_element_by_xpath
,但出了问题。(我尝试了CSS选择器和链接文本,但也出了问题(。
我尝试过的东西:
nav.find_element_by_css_selector('.btn-lg').click()
Error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".btn-lg"}
nav.find_element_by_xpath('/html/body/div[4]/div/div[2]/div/div/div[3]/div/div[2]/div[1]/div/div/form/div[1]/button').click()
Error: NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"/html/body/div[4]/div/div[2]/div/div/div[3]/div/div[2]/div[1]/div/div/form/div[1]/button"}
按钮中的HTML:
<button type="button" class="btn btn-default btn-lg" title="Filtros" ng-click="openFilters()" ga-event="" ga-label="open-orders-filters"><span class="uim-filter-count badge">1</span> <i class="icon-filter"></i></button>
和
<div class="uim-btn-filter uim-filter-checked" ng-class="{ 'uim-filter-checked': filters.getAppliedFilters().length }"><button type="button" class="btn btn-default btn-lg" title="Filtros" ng-click="openFilters()" ga-event="" ga-label="open-orders-filters"><span class="uim-filter-count badge">1</span> <i class="icon-filter"></i></button></div>
试试这个
driver.find_element_by_xpath(".//button[@title='Filtros']").click()
试试这个Xpath
'//button[@ng-click="openFilters()"]'
或者这个
'//button[@title="Filtros()"]'
或
'//button[@title="Filtros()" and (@ng-click="openFilters()")]'
如果您的元素在iframe中,则需要执行以下操作:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait as wait
wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_tag_name("iframe")))
我根据iframe的标签名称在这里提到了它。根据适合您的实际定位器,您可能需要使用其他定位器。