如何单击使用<;创建的按钮;span>;SeleniumWebDriver的标签?

如何单击使用<;创建的按钮;span>;SeleniumWebDriver的标签?,selenium,selenium-webdriver,selenium-firefoxdriver,Selenium,Selenium Webdriver,Selenium Firefoxdriver,我有一个span标签,看起来像html标签上的一个按钮 <span class="middle">Next</span> 使用绝对值 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'

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

    <span class="middle">Next</span>
使用绝对值

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中出现异常:找不到元素:{“方法”:“xpath”,“选择器”:”//span[包含(,,\“下一步\”)]“} 命令持续时间或超时:30.12秒 有关此错误的文档,请访问:

对于我尝试的所有方法,它都显示了相同的异常,选择器细节发生了变化。有人能帮我找到解决方案吗?这样我就可以在span标签中找到下一个按钮并单击它

下一个按钮位于iFrame中:下面是html的一部分,包含所需的span标记

下一个 我还尝试了:

driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
但有以下错误:

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

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

试试这个

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

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

我认为该元素位于帧或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();
 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();
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();
Edited1:-如果由于元素当前不可见而出现异常,则需要实现
WebDriverWait
以等待元素可见,如下所示:-

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();
 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();
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();
Edited2:-如果不幸无法看到,请尝试使用
JavascriptExecutor
单击它,如下所示:-

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();
 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();
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();

我认为该元素位于帧或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();
 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();
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();
Edited1:-如果由于元素当前不可见而出现异常,则需要实现
WebDriverWait
以等待元素可见,如下所示:-

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();
 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();
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();
Edited2:-如果不幸无法看到,请尝试使用
JavascriptExecutor
单击它,如下所示:-

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();
 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();
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();
试试这个

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

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

我试过了,它对我有效:

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

我试过了,它对我有效:

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

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