我正在尝试使用下面的代码自动将值插入弹出窗口内的字段(请参阅附图)。但是使用下面的自动化代码不会在字段中插入值。
如果有人可以提供相同的理由和解决方案,我们将不胜感激。
感谢任何帮助:
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;
import static org.junit.Assert.fail;
public class test9 {
private WebDriver driver;
private String baseUrl;
private boolean acceptNextAlert = true;
private StringBuffer verificationErrors = new StringBuffer();
private WebElement foundElement;
@Before
public void setUp() throws Exception {
driver = new FirefoxDriver();
baseUrl = "http://www.url.com/";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
@Test
public void test9() throws Exception {
driver.get(baseUrl + "/Login.aspx");
driver.findElement(By.id("dnn_ctr362_Login_Login_DNN_txtUsername")).clear();
driver.findElement(By.id("dnn_ctr362_Login_Login_DNN_txtUsername")).sendKeys("comp-da@comp.com");//dummy username
driver.findElement(By.id("dnn_ctr362_Login_Login_DNN_txtPassword")).clear();
driver.findElement(By.id("dnn_ctr362_Login_Login_DNN_txtPassword")).sendKeys("password");//dummy password
driver.findElement(By.id("dnn_ctr362_Login_Login_DNN_cmdLogin")).click();
driver.findElement(By.xpath("//div[@id='Nav']/span/span/div[3]/a")).click();
driver.findElement(By.id("dnn_cd_Banner_Customer_b")).click();
Thread.sleep(10000);
WebElement userName = find(By.xpath("//*[@id=\"dnn_ctr384_View_CreateDealerAdmin_email\"]"),"abc@gmail.com");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_firstname"),"Abc");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_lastname"),"R");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_firstname"),"test1");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_lastname"),"abc");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_password"),"password");
find(By.id("dnn_ctr384_View_CreateDealerAdmin_passwordconfirm"),"password");
search(By.id("dnn_ctr384_View_CreateDealerAdmin_create_b_input"));
driver.findElement(By.cssSelector("div.Controls")).click();
driver.findElement(By.id("dnn_dnnLogin_enhancedLoginLink")).click();
}
@After
public void tearDown() throws Exception {
driver.quit();
String verificationErrorString = verificationErrors.toString();
if (!"".equals(verificationErrorString)) {
fail(verificationErrorString);
}
}
private boolean isElementPresent(By by) {
try {
driver.findElement(by);
return true;
} catch (NoSuchElementException e) {
return false;
}
}
public WebElement find(By by,String keys){
By byy = by;
String key=keys;
System.out.println(by.toString()+keys.toString());
for (int milis=0; milis<3000; milis=milis+3000) {
try {
foundElement = driver.findElement(by);
//driver.findElement(by).clear();
driver.findElement(by).click();
Thread.sleep(5000);
foundElement.sendKeys("");
Thread.sleep(5000);
foundElement.sendKeys(keys);
Thread.sleep(5000);
foundElement.sendKeys(keys);
} catch (Exception e) {
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
return foundElement;
}
public WebElement search(By by){
By byy = by;
for (int milis=0; milis<3000; milis=milis+3000) {
try {
driver.findElement(by).click();
driver.findElement(by).click();
} catch (Exception e) {
try {
Thread.sleep(200);
} catch (InterruptedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
}
return foundElement;
}
private String closeAlertAndGetItsText() {
try {
Alert alert = driver.switchTo().alert();
if (acceptNextAlert) {
alert.accept();
} else {
alert.dismiss();
}
return alert.getText();
} finally {
acceptNextAlert = true;
}
}
}
修改
包括包含电子邮件,名字,姓氏等的html。
<div id="Heading">
<span id="dnn_ctr384_View_CreateDA">Create DA</span>
<span class="nyk_ButtonSpan">
</div>
<table id="dnn_ctr384_View_CreateDealerAdmin" class="nykTable" border="0">
<tbody>
<tr class="nykRow">
<td class="nykCreateLbl" style="vertical-align: top">
<td class="nykCreateEntry">
</tr>
<tr class="nykRow">
<tr class="nykRow">
<tr class="nykRow">
<tr class="nykRow">
<tr id="dnn_ctr384_View_CreateDealerAdmin_assigned2_row" class="nykRow">
<tr class="nykRow">
</tbody>
</table>
答案 0 :(得分:0)
而不是使用“警报alert = driver.switchTo()。alert()”语句,您将不得不使用“ driver.switchTo()。activeElement() “。这是你问题的解决方案。