【问题标题】:Unable to click checkbox inside <label for="chk"> tag using selenium无法使用硒单击 <label for="chk"> 标记内的复选框
【发布时间】:2017-04-30 22:44:51
【问题描述】:

我正在尝试单击数据单元格 td 中的复选框,但每次我得到:

org.openqa.selenium.ElementNotVisibleException

例外, 问题是元素已启用但不可见。

以下是页面来源:

<table id='table1'>
    <thead></thead>
    <tbody>
        <tr>
            <td>
            <input id='id1' class='class' type='checkbox'>
            <label for='chk1'>
            :: before
            </label>
            </td>
        </tr>
        <tr>
            <td>
            <input id='id2' class='class' type='checkbox'>
            <label for='chk2'>
            :: before
            </label>
            </td>
        </tr>
        <tr>
            <td>
            <input id='id3' class='class' type='checkbox'>
            <label for='chk3'>
            :: before
            </label>
        </td>
        </tr>
    </tbody>
</table>

但下面的代码可以正常工作并返回。

System.out.println("isDisplayed()"+ td_lblcollection.get(0).isDisplayed()); --> False
System.out.println("isEnabled()"+td_lblcollection.get(0).isEnabled()); --> True
System.out.println("isSelected()"+td_lblcollection.get(0).isSelected()); --> False

手动复选框可见且可以选择,但 IsDisplayed 为 false 并引发元素不可见异常。

尝试了以下所有方法,但一切都失败了。定位器很好。

定位器:

@FindBy(xpath = "//*[@id='table1']/tbody/tr")
List<WebElement> tblLookupSites;

方法1:在表中循环

for (WebElement trElement : tblLookupSites) {
    List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) {
    List<WebElement> td_lblcollection =tdElement.findElements(By.xpath("label"));
    td_lblcollection.get(0).click();
}
}  

方法2:在表中循环

for (WebElement trElement : tblLookupSites) {
    List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) {
    List<WebElement> td_lblcollection =tdElement.findElements(By.xpath("input"));
    td_lblcollection.get(0).click();
}
}  

方法3:在表中循环

for (WebElement trElement : tblLookupSites) {
    List<WebElement> td_collection = trElement.findElements(By.xpath("td"));
for (WebElement tdElement : td_collection) {
    List<WebElement> td_lblcollection =tdElement.findElements(By.id("id1"));
    td_lblcollection.get(0).click();
}
}  

请建议如何点击此复选框。

UI 是使用 vue.js 创建的。

【问题讨论】:

    标签: java selenium checkbox html-table vue.js


    【解决方案1】:

    尝试JavascriptExecutor界面通过像这样注入一些javascript来点击不可见元素-

    WebElement element = driver.findElement(By.xpath("//input[@id='id1']"));
    JavascriptExecutor js =(JavascriptExecutor)driver;
    js.executeScript("arguments[0].click();", element);
    

    如果可行,也请尝试其他方式-(这在我的项目中以相同的结构工作)-

    driver.findElement(By.xpath("//label[@for='chk1']")).click(); 
    

    【讨论】:

    • 我以前试过这个,现在又试了一次,还是不行。
    • 请试试这个,它可能会有所帮助 - WebElement element = driver.findElement(By.xpath("//input[@id='id1']")); JavascriptExecutor js =(JavascriptExecutor)driver; js.executeScript("arguments[0].click();", element);
    • 太棒了! Javascript 工作,非常感谢 Narendra _/|_。
    • @Leo,很高兴听到你的声音。请不要忘记接受答案:)
    【解决方案2】:

    使用下面的东西。

    driver.findElement(By.xpath("//*[@id='id1']")).click();

    【讨论】:

    • 这只有在您不更改文档中的任何内容时才有效
    猜你喜欢
    • 1970-01-01
    • 2022-10-30
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 2019-02-11
    • 1970-01-01
    相关资源
    最近更新 更多