【问题标题】:Check a Checkbox if it is not checked in Selenium Python如果在 Selenium Python 中未选中,请选中复选框
【发布时间】:2021-09-18 02:18:55
【问题描述】:

我确实有以下页面来源:

1. <li_ngcontent-shv-c123 mat-menu-item role="presentation" class="mat-focus-indicator dropdown-item mat-menu-item ng-star-inserted" tabindex="0" aria-disabled="false">
2. <mat-checkbox_ngcontent-shv-c123 class= "mat-checkbox example-margin mat-accent _mat-animation-noopable" id="53oo32be-6855-4yt4-965d-y71078b642">
3. <label class="mat-checkbox-layout" for="53oo32be-6855-4yt4-965d-y71078b642-input"> 
4. <div class="mat-checkbox-inner-container">.
5. <input type="checkbox" class="mat-checkbox-input cdk-visually-hidden" id="53oo32be-6855-4yt4-965d-y71078b642-input" tabindex="0" value="53oo32be-6855-4yt4-965d-y71078b642" name="Item List" aria-checked="true"> 
6. <div matripple="" class="mat-ripple mat-checkbox-ripple mat-focus-indicator">
7.  <div class="mat-ripple-element mat-checkbox-persistent-ripple">
8. </div>
9. </div>
10. <div class="mat-checkbox-frame">
11. </div>
12. <div class="mat-checkbox-background">
13. <svg version="1.1" focusable="false" viewBox="0 0 31 31" xml:space="preserve" class="mat-checkbox-checkmark">
14.  <path fill="none" stroke="white" d="M2.1,89.5 9,78.6 3.3,77.1" class="mat-checkbox-checkmark-path">
15. </Path>
16. </svg><div class="mat-checkbox-mixedmark">
17. </div>
18. </div>
19. </div>
20. <span class="mat-checkbox-label">
21. <span style="display none;">&nbsp;</span>XYZ Element</span>
22. </label> 
23. </mat-checkbox><div matripple="" class="mat-ripple mat-menu-ripple">
24. </div>
25. </li>

在这个项目列表(第 5 行)中是一个下拉列表,其中包含有限数量的带有复选框的元素。 我需要检查的是下拉列表中的元素 XYZ Element (Line21),当且仅当复选框未选中时,才使用它检查复选框。

如果该复选框已被选中,请保持原样。

我尝试了以下代码:

test= "//span[contains(text(),'XYZ Element')]"

try:

driver.finde_element_by_xpath(test).is_selected()
pass

except:
driver.find_element_by_xpath(test).click()

上面的代码没有产生需要的结果。

有没有其他方法可以做到这一点?

提前致谢。

【问题讨论】:

    标签: python selenium testing xpath automation


    【解决方案1】:

    使用find_elements查看列表中是否有任何内容,如果有,您可以单击或检查是否单击如下:

    代码:

    try:
        if len(driver.find_elements_by_xpath("//span[contains(text(),'XYZ Element')]/../descendant::input")) > 0 :
            print("Element is present")
            if driver.find_element_by_xpath("//span[contains(text(),'XYZ Element')]/../descendant::input").is_selected():
                print("it's already selected")
            else:
                driver.find_element_by_xpath("//span[contains(text(),'XYZ Element')]/../descendant::input").click()
        else:
            print("Element is not present at all")
    except:
        print("Check out the code again")
        pass
    

    更新 1:

    使xyz element 变量:

    some_str = "XYZ Element"
    driver.find_element_by_xpath(f"//span[contains(text(),'{some_str}')]")
    

    【讨论】:

    • 我已经用几个检查点再次更新了代码,让我知道这是否有效
    • 非常感谢....
    • 在其他部分test= "//span[contains(text(),'XYZ Element')]"else:driver.find_element_by_xpath(test).click()我把这个
    • test= "//span[contains(text(),'XYZ Element')]" 有没有办法将 XYZ 元素 设为变量?我需要一个变量才能在测试中工作。有什么具体的语法吗?
    • 我已经在更新 1 部分更新了上面的代码。
    猜你喜欢
    • 2013-12-06
    • 1970-01-01
    • 1970-01-01
    • 2016-10-15
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-16
    • 1970-01-01
    相关资源
    最近更新 更多