【问题标题】:How to click a Span element?如何单击 Span 元素?
【发布时间】:2022-01-06 19:58:19
【问题描述】:

由于某种原因,当我在 selenium ide 中使用 click 命令时,以下 xpath 不会点击。我试图点击的链接称为设备。我怎样才能让它点击?

xpath=//div[2]/section/nav/div[3]/div/div/span

这里是完整的代码:

<div title="Devices" class="asm-source-list-group-view">    <header role="heading" aria-hidden="true">Devices</header>    <div><div role="link" aria-selected="true" title="Devices" class="asm-source-list-item-view cw-being-hovered">    <span class="title" aria-hidden="true">Devices</span>    <span class="count cw-hidden" aria-hidden="true">0</span></div><div role="link" aria-selected="false" title="Assignment History" class="asm-source-list-item-view">    <span class="title" aria-hidden="true">Assignment History</span>    <span class="count cw-hidden" aria-hidden="true">0</span></div></div></div>

【问题讨论】:

  • 这段代码不完整。第一个 /div、section 或 /nav 在哪里?

标签: html selenium selenium-ide


【解决方案1】:

如果您收到此元素当前不可见或其他错误,您必须执行WebDriverWait 直到该元素可见。

所以试试这个

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class="asm-source-list-group-view"]/header/div[@class="asm-source-list-item-view cw-being-hovered"]/span")));
element.click();

当您提供 Xpath 时,请尝试提供正确的类名。

Xpath = //div[@class="asm-source-list-group-view"]/header/div[@class="asm-source-list-item-view cw-being-hovered"]/span

【讨论】:

    【解决方案2】:

    要单击元素,您可以使用以下 XPath:

    //div[@title="Devices"]//div[@title="Devices" and @class="asm-source-list-item-view cw-being-hovered"]//span
    

    【讨论】:

      【解决方案3】:

      假设文本设备是唯一的, 你可以使用Xpath as //span[contains(text(),'Devices'] 然后使用单击此。它应该适用于driver.findElement(By.Xpath("//span[contains(text(),'Devices']").click();

      【讨论】:

        【解决方案4】:

        检查你给她的 Xpath 是否是唯一的,如果它是唯一的并且它仍然想要点击我建议你尝试这 3 种方法来点击我,其中一种总是可以完成这项工作:

            Using Actions Lib :
        
        WebElement elemCN = driver.findElement(By.xpath("your path"));
                int widthCN = elemCN.getSize().getWidth();
                act.moveToElement(elemCN).moveByOffset((widthCN / 2) - 2, 0).click().perform();
        

        使用 JS:

        JavascriptExecutor js = (JavascriptExecutor) driver;
        WebElement SubmitPaiement = driver.findElement(By.xpath("your path"));
        js.executeScript("arguments[0].click();", SubmitPaiement);
        

        最后是正常的.click()

        【讨论】:

          猜你喜欢
          • 2017-11-23
          • 2014-09-13
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-10-11
          • 1970-01-01
          相关资源
          最近更新 更多