gpt4 book ai didi

python - 使用 Python/Selenium 从 Angular 网站中选择复选框

转载 作者:行者123 更新时间:2023-12-02 19:19:27 29 4
gpt4 key购买 nike

Here's the angular source

尝试创建一个简单的脚本,用 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">
&nbsp;&nbsp;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">
&nbsp;&nbsp;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元素,以便单击与文本关联的复选框,例如日期时间等,您必须引发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

关于python - 使用 Python/Selenium 从 Angular 网站中选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53654251/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com