【问题标题】:find a checkbox from a tr tag and select it in Selenium(java)从 tr 标签中找到一个复选框并在 Selenium(java) 中选择它
【发布时间】:2016-03-27 18:13:03
【问题描述】:

我使用以下代码来检索我想在 selenium 中选择的行:

WebElement row = driver.findElement(By.xpath("//tr[contains(.,'prod1')]"));

结果,我得到以下行:

<th width="18px">
                <input class="selectedProducts" id="checkbox-37640" onclick="markedProductsHandler.toggleMarkProduct(this,37640)" type="checkbox">
            </th>


            <th style="white-space:nowrap; width:573px;" align="left">

<a class="maxTextSize" style="width:530px" href="product.html?id=37640&amp;rm7zz5c=ZBOeBCOAs">aa prod1</a>
            </th>

            <th style="white-space:nowrap;" align="right" width="140px">

                    <a href="product-edit.html?id=37640&amp;rm7zz5c=ZBOeBCOAs"><img src="_images/icons/pencil.png?rm7zz5c=ZBOeBCOAs" title="Edit"></a> &nbsp;


                        <input name="favoriteid" class="favoriteAction" value="37640" type="hidden">
                        <input name="favoriteaction" class="favoriteAction" value="add" type="hidden">
                        <a href="javascript:" onclick="onFavoriteAction(this)">
                            <img src="_images/icons/star_grey.png?rm7zz5c=ZBOeBCOAs" alt="Watch product" title="Watch product">
                        </a> &nbsp;

        <form action="product-pdf.html?rm7zz5c=ZBOeBCOAs" method="post">
            <input name="export" value="pdf" type="hidden">
            <input name="id" value="37640" type="hidden">
            <a href="javascript:" onclick="javascript: $(this).parent().submit();"><img src="_images/icons/page_white_acrobat.png?rm7zz5c=ZBOeBCOAs" title="Download as PDF"></a> &nbsp;
        </form>
            </th>

现在我想选中复选框:

row.findElement(By.xpath("//input[@type='checkbox']")).click();

但我收到以下错误:

org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

有人可以帮我吗?谢谢。

【问题讨论】:

  • 根据异常,似乎复选框被隐藏了。类selectedProductsvisibility:hidden;display:none之类的样式吗?github.com/SeleniumHQ/selenium/blob/master/java/client/src/org/…
  • 你能告诉我们row.findElement(By.xpath("//input[@type='checkbox']")).isDisplayed()的结果吗? + 尝试添加等待复选框可见的服务员
  • 您好,结果为假
  • 在您的示例 HTML 中,tr 到底在哪里?一切都是th
  • 是的,它们看起来像这样,selenium 找到并返回这个 html。不好吗?

标签: java html selenium testing checkbox


【解决方案1】:

试过 fluentWait 吗?

WebDriverWait wait = new WebDriverWait(driver, timeout);
wait.until(ExpectedConditions.visibilityOf
               (driver.findElement(By.xpath("//input[@type='checkbox']"))));

检查是否会抛出异常。

【讨论】:

  • 嗨,这是结果 org.openqa.selenium.TimeoutException: Timed out after 5 seconds waiting for visibility of [[FirefoxDriver: firefox on WINDOWS ] -> xpath: //input[@type= '复选框']]
【解决方案2】:

尝试添加显式等待直到元素可见。如果元素被永久隐藏(出于某种原因?)请尝试以下操作:

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

如果一切都会失败的想法之一:您可以通过javascript触发以下方法:markedProductsHandler.toggleMarkProduct

【讨论】:

  • :(最后机会:你能分享页面链接吗?
  • 谢谢,但这是不可能的。它有什么帮助?
  • 好的,没关系。也许您错过了问题描述中的某些内容。也许页面上有 2 个元素,而您发现了不正确的一个。阅读:stackoverflow.com/help/mcve
  • 我提供了返回的行(web元素),还不够吗?
  • 对不起,没有可重复的例子,我无能为力。
【解决方案3】:

我认为复选框的 id 是动态的,所以你应该像下面这样生成 xpath:

row.findElement(By.xpath("//input[@id[starts-with(., 'checkbox')]]")).click();

【讨论】:

  • 谢谢,但我仍然收到错误 org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互
猜你喜欢
  • 1970-01-01
  • 2012-12-22
  • 1970-01-01
  • 2013-12-07
  • 2017-09-07
  • 1970-01-01
  • 2017-05-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多