【问题标题】:Select a row element based on another row element in Selenium根据 Selenium 中的另一个行元素选择一个行元素
【发布时间】:2021-10-13 16:46:49
【问题描述】:

我想根据 Selenium 中行中的另一个元素选择一个复选框。 eg:如何选择Cricket对应的复选框?

<body>
    <table id="MyTable">
        <tbody>
            <tr>
                <td>
                    <input type="checkbox" />
                </td>
                <td>Football</td>
            </tr>
            <tr> 
                <td>
                    <input type="checkbox" />
                </td>
                <td>Cricket</td>
            </tr>
        </tbody>
    </table>
</body>

【问题讨论】:

    标签: java selenium selenium-webdriver xpath ui-automation


    【解决方案1】:

    尝试以下 xpath:

    driver.findElement(By.xpath("//table[@id='MyTable']//tr[contains(., 'Cricket')]//input[@type='checkbox']")).click();
    

    【讨论】:

      【解决方案2】:

      看到的复选框一般都是input标签,如果你想用Cricket关键字来选择,你可以使用下面的xpath

      //td[text()='Cricket']/preceding-sibling::td/input
      

      代码中:-

      driver.findElement(By.xpath("//td[text()='Cricket']/preceding-sibling::td/input")).click();
      

      您也可以尝试使用显式等待:

      new WebDriverWait(driver, 10).until(ExpectedConditions.elementToBeClickable(By.xpath("//td[text()='Cricket']/preceding-sibling::td/input"))).click();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-10-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-11
        相关资源
        最近更新 更多