Java 具有动态可见性的不可拾取元素/动作
我正在为InternetExplorer9上的一个项目评估Selenium2,我选择了mercedes-benz网站,因为它使用AJAX和一些内联弹出窗口,所以我编写了一个小测试Java 具有动态可见性的不可拾取元素/动作,java,internet-explorer,webdriver,selenium-webdriver,Java,Internet Explorer,Webdriver,Selenium Webdriver,我正在为InternetExplorer9上的一个项目评估Selenium2,我选择了mercedes-benz网站,因为它使用AJAX和一些内联弹出窗口,所以我编写了一个小测试 转到梅赛德斯-奔驰 单击“A”,然后在显示的覆盖图中选择“KonFigulator” 点击“Weiter>” 点击“>Rateändern”,弹出窗口将显示 单击弹出窗口中的“i”按钮 我的示例代码中有两个问题: package org.test.demo; import org.openqa.selenium.
package org.test.demo;
import org.openqa.selenium.*;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;
@Test
public class TestStep_DebugChangeRate {
public void test() {
driver.manage().deleteAllCookies();
driver.get("http://www.mercedes-benz.ch");
WebElement btnA = driver.findElement(By.xpath("//a[.='A']"));
// first problem, i cannot combine moveToElement(btnA) and
// moveToElement(btnKonfigurator), because btnKonfigurator is not
// visible at the moment of building the Action which will then fail
// in NoSuchElement, that's why I cheat with sendKeys(), any tips?
(new Actions(driver))
.moveToElement(btnA)
.click(btnA)
.sendKeys(Keys.TAB)
.sendKeys(Keys.TAB)
.sendKeys(Keys.TAB)
.sendKeys(Keys.ENTER)
.build()
.perform();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement btnContinue = driver.findElement(By.id("vsAppLnkContinue1"));
btnContinue.click();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement btnChangeRate = driver.findElement(By.id("vsAppLnkPcnChangeRate"));
btnChangeRate.click();
performWaitBool(driver, PerformWaitExpectedConditions.isPageLoadFinished(), "timeout waitForPageLoad");
WebElement frameInline = driver.findElement(By.xpath("//div[@class='contentsC']/iframe"));
WebDriver frame = driver.switchTo().frame(frameInline);
WebElement btnInfoPpi = frame.findElement(By.xpath("//a[contains(@onclick, 'InsurancePpi')]/img"));
// does not throw an error, though the info popup is not opened
btnInfoPpi.click();
Boolean isDisplayed = btnInfoPpi.isDisplayed(); // true
int elementWidth = btnInfoPpi.getSize().getWidth(); // 23
btnInfoPpi.sendKeys(Keys.Enter); // this will open the popup however
}
}
- 第一:如何构建包含动态内容的操作链,而动态内容在构建链时还不可用 (即moveToElement(A)。moveToElement(B):A使B可见,因此在构建时,B不存在)
- 第二:这是包含在a元素中的img元素,我要单击:
关于您的img问题,我将尝试通过在a标记内部执行相关的js
((JavascriptExecutor) driver).executeScript("script;");
此外,我无法帮助,因为该页面不适合我。
尝试使用Selenium IDE获取操作的核心。将其导出为Java Junit4 webdriver testcase。这可能会帮助您找到解决方案。我主要担心的是Selenium Webdriver在这里出了问题。因为我正在测试一个网站,所以我不能执行脚本,因为我需要模拟一个用户,所以它必须是点击图片。如果图像周围没有标记,测试将通过,但它不正确。Selenium IDE也做了同样的事情,只需标识元素并发送一个.click()。由于某种神秘的原因,它与selenium不起作用:(执行用户按下按钮时执行的脚本与用户按下按钮时执行的脚本是一样的。这当然是一种解决办法。我对IE测试没有那么坚定,因为对我来说Firefox工作得更好;-)对于IDE,你无法真正说出它在幕后做了什么。因为它生成的代码通常与IDE测试的工作方式不同。
((JavascriptExecutor) driver).executeScript("script;");