【问题标题】:How can I click div button in Selenium Java如何单击 Selenium Java 中的 div 按钮
【发布时间】:2017-02-23 18:56:35
【问题描述】:

有谁知道我如何使用以下 HTML 代码单击(在 Java 中)按钮?

<div role="button" id=":t5.ss" class="c-N-K a-b a-b-va KMD69e-bU2Jkc-b DF" tabindex="0" aria-label="Join as John" style="user-select: none;">Join</div>

我在 Java 中的 sn-p 代码:

driver.get("https://www.somepage.com");
... enter new tab ...
Thread.sleep(10000);
driver.findElement(By.xpath("//div[@role='button']")).click();

我有

Exception in thread "main" org.openqa.selenium.NoSuchElementException:
no such element: Unable to locate element
{"method":"xpath","selector":"//div[@role='button']"}

我也尝试过没有成功:

driver.findElement(By.xpath("//div[@id=':t5.ss']")).click();

driver.findElement(By.xpath("//div[@aria-label='Join as John']")).click();

driver.findElement(By.cssSelector("div[id=':t5.ss']")).click();

【问题讨论】:

  • 你的问题不是点击,而是找到了。
  • 你试过什么? aria-labelrole,甚至是text() 使用xpath,或者一些组合可能都可以正常工作。我可以给出更明确的答案,但我不确定您目前尝试了什么。
  • 你有没有等待显示/可见的东西?
  • 是的,我已经等了 10 秒
  • driver.findElement(By.xpath("//div[@id=':t5.ss']")).click();这些线路中的任何一条都没有等待

标签: java selenium button web driver


【解决方案1】:

Selenium 具有名为 By.id() 和 By.className() 的方法来查找按钮并使用 click() 来单击选定的按钮。

driver.findElement(By.id("element id")).click()

driver.findElement(By.className("element class")).click()

【讨论】:

    【解决方案2】:

    要点击一个元素,它必须是可见和显示的。

    WebElement e = find(...); //POM搜索

    if (e != null){
                    Thread.sleep(...);
                    while (!e.isDisplayed())
                            Thread.sleep(...);
                    e.click();
    }
    

    这对我很有帮助。

    【讨论】:

    • 这个元素是可见的,我确定
    • 有更好的方法来等待元素可见。例如:new WebDriverWait(driver, timeout).until(ExpectedConditions.visibilityOfElementLocated(by))
    • 只是一个额外的间接层,会降低吞吐量。但如果你喜欢那样的话。
    • @efekctive 当e == null 因为它还没有加载到DOM 中时,你的代码会发生什么?你的Thread.sleeps 不会被触发,对吧? WebDriverWait 是首选方法的原因有很多。适当的超时,没有线程阻塞,更通用的条件等待。您可以在我在回答中提供的链接中阅读更多相关信息。您还可以使用WebDriverWait 来实例化WebElement,例如:WebElement element = new WebDriverWait(driver, timeout).until(ExpectedConditions.elementToBeClickable(by)); 仅当元素可点击时才返回。
    • 如果你碰巧读到了代码 if (e !=null) 意味着它永远不会为空。我已经发现了异常。在您认为自己有答案之前,请阅读并理解。所以请理解发布的代码。其次了解你使用的工具。我不会为反对票而烦恼。这是浪费时间
    【解决方案3】:

    在您的测试中使用Thread.sleep 是一种不好的做法,因为您永远不会知道您要使用的页面和元素是否实际加载完毕。

    最好使用显式等待。这是一个例子:

    new WebDriverWait(driver, timeout).until(ExpectedConditions.visibilityOfElementLocated(by));

    更多相关信息可以found here

    NoSuchElementException 指向三种可能性:
    a) 根本没有您试图在 HTML
    中找到的此类元素 b) 元素尚未加载,如果是,请使用 WebDriverWait
    c) 该元素是 iframe 的一部分,除非您切换到该 iframe,否则无法找到该元素

    如果 c) 正确,您可以使用以下命令切换到 iframe: driver.switchTo().frame(d().findElement(iframe));

    【讨论】:

    • 是的,罗伯特,我想 c) 选项是原因。我添加了以下代码: ArrayList tabs = new ArrayList (driver.getWindowHandles()); driver.switchTo().window(tabs.get(1)); driver.findElement(By.xpath("//div[@role='button']")).click(); --- 但是之后有这个异常:线程“main”中的异常 org.openqa.selenium.ElementNotVisibleException:元素不可见
    • 所以它不在 iFrame 中,而是在另一个选项卡中。我在写答案时没有考虑到这一点,但我很高兴我给了你正确的想法。
    • 那么 driverwait 如何在没有睡眠的情况下等待,只是旋转循环?如果检查 e.isDisplayed() 返回 true 是否已加载元素?废话太多
    【解决方案4】:

    尝试使用 sendKeys(),而不是 click()。

    例子:

    myDiv.sendKeys(Keys.Enter); // or Keys.Return
    

    【讨论】:

      【解决方案5】:

      您应该使用 id 来标识元素 driver.findElement(By.id(":t5.ss")).click();

      还可以通过isDisplayed()和isEnabled()点击检查元素是否显示和启用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-01-29
        • 2013-05-07
        • 1970-01-01
        • 1970-01-01
        • 2016-01-30
        • 2012-01-19
        • 2016-03-02
        相关资源
        最近更新 更多