使用来自 angular 网站的 Python/Selenium 到 select 复选框
Using Python/Selenium to select checkboxes from an angular website
尝试创建一个简单的脚本,其中 checks/unchecks 一些盒子 python/selenium。
我已经扔掉了我的那本小书,但无法正确识别元素。我试过(可能不正确)css、xpath、标签、文本。
如有任何帮助,我们将不胜感激。
代码试用:
driver.find_elements_by_class_name('list-group-items')[1].click()
HTML:
<div class="col-sm-12">
<fieldset>
<legend>
Step 2: Choose Data fields
</legend>
<div class="col-sm-5 col-lg-4">
<div class="panel minimal minimal-gray">
<div class="panel-title">
<h4>
Standard Fields:
</h4>
</div>
<ul class="list-group scrollable ng-scope" id="standard-fields" ng-class="{error: ctrl.export.errors.columns}">
<!-- ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
<input type="checkbox" ng-model="item.checked" class="ng-pristine ng-untouched ng-valid">
Date
</li><!-- end ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
<input type="checkbox" ng-model="item.checked" class="ng-pristine ng-untouched ng-valid">
Time
</li><!-- end ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
看来你很接近。根据你的代码试验而不是 find_elements_by_class_name('list-group-items')[1]
,使用 find_element_by_class_name('list-group-item')
会让你更接近。
但是,由于所需的元素是 Angular 元素,因此单击与文本关联的 复选框 ,例如Date、Time 等,您必须为 元素引入 WebDriverWait可点击,您可以使用以下任一解决方案:
日期:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='list-group-item ng-binding ng-scope list-group-item-success' and contains(@ng-repeat,'exportFormats')][contains(.,'Date')]/input[@class='ng-pristine ng-untouched ng-valid']"))).click()
时间:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='list-group-item ng-binding ng-scope list-group-item-success' and contains(@ng-repeat,'exportFormats')][contains(.,'Time')]/input[@class='ng-pristine ng-untouched ng-valid']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
尝试创建一个简单的脚本,其中 checks/unchecks 一些盒子 python/selenium。
我已经扔掉了我的那本小书,但无法正确识别元素。我试过(可能不正确)css、xpath、标签、文本。
如有任何帮助,我们将不胜感激。
代码试用:
driver.find_elements_by_class_name('list-group-items')[1].click()
HTML:
<div class="col-sm-12">
<fieldset>
<legend>
Step 2: Choose Data fields
</legend>
<div class="col-sm-5 col-lg-4">
<div class="panel minimal minimal-gray">
<div class="panel-title">
<h4>
Standard Fields:
</h4>
</div>
<ul class="list-group scrollable ng-scope" id="standard-fields" ng-class="{error: ctrl.export.errors.columns}">
<!-- ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
<input type="checkbox" ng-model="item.checked" class="ng-pristine ng-untouched ng-valid">
Date
</li><!-- end ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
<input type="checkbox" ng-model="item.checked" class="ng-pristine ng-untouched ng-valid">
Time
</li><!-- end ngRepeat: item in ctrl.exportFormats.columns track by item.code --><li ng-repeat="item in ctrl.exportFormats.columns track by item.code" class="list-group-item ng-binding ng-scope list-group-item-success" ng-class="{'list-group-item-success' : item.checked}">
看来你很接近。根据你的代码试验而不是 find_elements_by_class_name('list-group-items')[1]
,使用 find_element_by_class_name('list-group-item')
会让你更接近。
但是,由于所需的元素是 Angular 元素,因此单击与文本关联的 复选框 ,例如Date、Time 等,您必须为 元素引入 WebDriverWait可点击,您可以使用以下任一解决方案:
日期:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='list-group-item ng-binding ng-scope list-group-item-success' and contains(@ng-repeat,'exportFormats')][contains(.,'Date')]/input[@class='ng-pristine ng-untouched ng-valid']"))).click()
时间:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//li[@class='list-group-item ng-binding ng-scope list-group-item-success' and contains(@ng-repeat,'exportFormats')][contains(.,'Time')]/input[@class='ng-pristine ng-untouched ng-valid']"))).click()
注意:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC