Selenium无法找到一个类

问题描述 投票:0回答:1

所以我在使用selenium的时候遇到了一个定位类的问题,我已经尝试了所有的方法来成功地定位类的属性,并对它进行一些操作,例如。

driver.find_element_by_tag_name('div button')
driver.web.find_element_by_class_name('btn-secondary-md save-button ng-binding')

例如: 代码样本

<button class="btn-secondary-md save-button ng-binding" ng-click="$ctrl.showChangeOwnerModal()" ng-bind="'Label.ChangeOwner' | translate">Change Owner</button>
python selenium html-parsing
1个回答
0
投票

尝试用css选择器定位。

driver.find_element_by_css_selector('button.btn-secondary-md.save-button.ng-binding')

如果你还是得到一些错误,比如元素不可点击,尝试在webelement上使用显式等待。


0
投票

很可能是由于同步问题,你可以使用WebDriverWait来避免同步问题。

解决办法1:使用WebDriverWait来避免同步问题。

wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "btn-secondary-md save-button ng-binding")))

解决办法2.使用WebDriverWait来避免同步问题。

wait = WebDriverWait(driver,30)
element = wait.until(EC.element_to_be_clickable((By.XPATH, "//button[contains(text(),'Change Owner')]")))

注意:请在你的解决方案中添加以下导入。 请在你的解决方案中添加以下导入

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

如果你仍然面临这个问题,那么请检查你的元素是否在iframe中,如果是,那么你必须切换到iframe控制。

© www.soinside.com 2019 - 2024. All rights reserved.