如何单击使用 <span> 标签为 Selenium webdriver 创建的按钮?
How to click a button created using <span> tag for Selenium webdriver?
我有一个看起来像 html 标签
上的按钮的 span 标签
<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']"));
没有成功!!它抛出以下异常:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"}
Command duration or timeout: 30.12 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
对于我尝试过的所有方法,它都显示了相同的异常,只是选择器详细信息发生了变化。有人可以帮我找到解决方案,以便我可以找到 span 标签中的“下一步”按钮并单击它。
下一个按钮在 iFrame 中:下面是 html 覆盖所需 span 标记的部分。
下一个
我也试过 :
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
但抛出以下错误:
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
如果需要我遗漏的东西,请告诉我..
试试这个....
driver.findElement(By.xpath("//span[contains(@class,'middle') and contains(text(), 'Next')]"))
我认为这个元素在 frame 或 iframe 中,如果是,那么你需要在找到元素之前切换那个 frame 或 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();
试试这个
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();
我有一个看起来像 html 标签
上的按钮的 span 标签 <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']"));
没有成功!!它抛出以下异常:
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//span[contains(., \"Next\")]"} Command duration or timeout: 30.12 seconds For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
对于我尝试过的所有方法,它都显示了相同的异常,只是选择器详细信息发生了变化。有人可以帮我找到解决方案,以便我可以找到 span 标签中的“下一步”按钮并单击它。
下一个按钮在 iFrame 中:下面是 html 覆盖所需 span 标记的部分。
下一个我也试过 :
driver.switchTo().frame("iframe-applicationname_ModalDialog_0");
WebElement el = driver.findElement(By.cssSelector("span.middle"));
但抛出以下错误:
Caused by: org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with
如果需要我遗漏的东西,请告诉我..
试试这个....
driver.findElement(By.xpath("//span[contains(@class,'middle') and contains(text(), 'Next')]"))
我认为这个元素在 frame 或 iframe 中,如果是,那么你需要在找到元素之前切换那个 frame 或 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();
试试这个
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();