我正在评估Selenium2在Internet Explorer 9上的项目,我选择了梅赛德斯 - 奔驰网站,因为它使用了AJAX和一些内联弹出窗口,所以我写了一点测试。
我的示例代码中有2个问题:
第一:如何使用构建链时尚未提供的动态内容构建动作链 (即moveToElement(A).moveToElement(B):A使B可见,因此在构建时,B不存在)
第二:这是一个包含在a元素中的img元素,我想点击:
<a onclick="var cf = function(){ openInfo('InsurancePpi'); return false;}; var oamSF = function(){ return oamSubmitForm('calcForm','calcForm:j_id31');}; return (cf()==false) ? false : oamSF();" href="#"> <img src="images/mb/btn_info_rb.gif"> </a>
但.click()没有做任何事情。点击似乎已执行,但是应该显示弹出窗口没有任何反应。如果我使用.sendKeys(Keys.Enter)而不是.click()它可以正常工作。
我的示例代码:
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
}
}
答案 0 :(得分:0)
关于img的问题,我会尝试通过
从标签内部执行相关的js((JavascriptExecutor) driver).executeScript("script;");
此外,我无法帮助因为页面不适合我。 尝试使用Selenium IDE来获取您的操作的核心。导出为Java Junit4 webdriver testcase。这可能会帮助您找到解决方案。