【问题标题】:How to click on GWT enabled elements using Selenium and Python如何使用 Selenium 和 Python 点击​​启用 GWT 的元素
【发布时间】:2020-07-13 17:01:21
【问题描述】:

我作为新手和新手开发人员与 Selenium 一起工作。 我尝试解决这个 XPath 但没有结果,这就是我寻求帮助的原因。

所以我想点击具有动态 id 的复选框,但根据 tittle="Viewers" 找到这个复选框并不容易

请注意,在我想要包含在我的测试中的 div 中的复选框右侧有一列带有名称的复选框。

HTML:

<span class="row-selection"><input type="checkbox" value="on" id="gwt-uid-2215" tabindex="0"><label for="gwt-uid-2215"></label></span> <div class="row-label" title="Viewers">Viewers</div>

快照;

【问题讨论】:

  • 按 id 最好... id 是随机的吗?
  • 是的 id 是随机的

标签: python selenium xpath webdriverwait expected-condition


【解决方案1】:

所需元素是启用了GWT 的元素,因此要单击该元素,您必须为element_to_be_clickable() 诱导WebDriverWait,并且您可以使用以下基于Locator Strategy 之一:

  • 基于title属性使用xpath

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@title='Viewers']//preceding::span[1]//label"))).click()
    
  • 使用基于innerHTMLxpath

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Viewers']//preceding::span[1]//label"))).click()
    
  • 注意:您必须添加以下导入:

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

【讨论】:

    猜你喜欢
    • 2020-11-06
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-16
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多