【问题标题】:How to click a button created using <span> tag for Selenium webdriver?如何单击为 Selenium webdriver 使用 <span> 标签创建的按钮?
【发布时间】:2016-12-08 02:02:00
【问题描述】:

我有 span 标签,看起来像 html 标签上的一个按钮

    <span class="middle">Next</span>

我尝试过使用

    xpath=driver.findElement(By.xpath(".//*[@id='modal-actions-panel']/div[2]/a/span/span/span")); // by considering fixed id as reference

使用绝对的

xpath=driver.findElement(By.xpath("html/body/div[4]/div[2]/a/span/span/span"));  // took this from firebug

和使用

driver.findElement(By.cssSelector("span[class='middle']"));

没有成功!!它抛出异常:

线程 "main" org.openqa.selenium.NoSuchElementException 中的异常:无法找到元素:{"method":"xpath","selector":"//span[contains(., \"Next\") ]"} 命令持续时间或超时:30.12 秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html

对于我尝试的所有方法,它都显示了相同的异常,但选择器详细信息发生了变化。有人可以帮我找到解决方案,以便我可以找到 span 标签中的 Next 按钮并单击它。

下一步按钮在 iFrame 中:下面是覆盖所需 span 标签的 html 部分。

下一个

我也试过了:

driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));

但抛出以下错误:

原因:org.openqa.selenium.ElementNotVisibleException:元素当前不可见,因此可能无法与之交互

如果需要我缺少的东西,请告诉我..

【问题讨论】:

  • CSS 选择器看起来不错。验证 xpath:使用 firefox 手动打开页面。按 F12 打开开发工具并切换到控制台。键入 $x("//span[@class='middle']").length 以查看该 Xpath 匹配了多少元素。如果 Selenium 找不到它,您可能正在查看错误的窗口或框架。
  • 我可以看到计数为 0。但是当我右键单击 Next 按钮并检查元素时,我只能看到 Next 和 xpath(s)正如刚才提到的。我该如何解决这个问题?
  • 你找的是frame还是iframe??这个元素可以在任何框架或 iframe 内吗?验证它..
  • 你能贴出准确的 HTML 代码吗?
  • 只添加 html 的必需部分,

标签: selenium selenium-webdriver selenium-firefoxdriver


【解决方案1】:

试试这个....

driver.findElement(By.xpath("//span[contains(@class,'middle') and contains(text(), 'Next')]"))

【讨论】:

    【解决方案2】:

    我认为这个元素在框架或 iframe 内,如果是,那么您需要在找到元素之前切换该框架或 iframe 如下:-

    driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
    WebElement el = driver.findElement(By.cssSelector("span.middle"));
    el.click();
    
    //Now after all your stuff done inside frame need to switch to default content 
    driver.switchTo().defaultContent();
    

    Edited1 :- 如果由于元素当前不可见而出现异常,则需要实现 WebDriverWait 以等待元素可见,如下所示:-

     WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//span[@class = 'middle' and contains(text(), 'Next')]")));
    el.click();
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    

    Edited2 :- 如果不幸看不到它,请尝试使用JavascriptExecutor 点击它,如下所示:-

    WebDriverWait wait = new WebDriverWait(driver, 10);
    
    //Find frame or iframe and switch
    wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt("iframe-applicationname_ModalDialog_0"));
    
    //Now find the element 
    WebElement el = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//span[@class = 'middle' and contains(text(), 'Next')]")));
    
    //Now click using JavascriptExecutor
    ((JavascriptExecutor)driver).executeScript("arguments[0].click()" el); 
    
    //Once all your stuff done with this frame need to switch back to default
    driver.switchTo().defaultContent();
    

    【讨论】:

      【解决方案3】:

      我试过了,它对我有用:

      WebElement actionBtn=driver2.findElement(
        By.xpath("//span[contains(@class,'v-menubar-menuitem-caption')
        and contains(text(), 'Actions')]")
      );
      actionBtn.click();
      

      【讨论】:

        【解决方案4】:

        试试这个

        new WebDriverWait(driver, 30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@class,'middle') and contains(text(), 'Next')]"))).click();
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2023-03-16
          • 1970-01-01
          • 2017-10-31
          • 2014-04-04
          • 1970-01-01
          • 2020-09-04
          • 2019-12-14
          • 1970-01-01
          相关资源
          最近更新 更多