我尝试了几个选项来使用Selenium处理弹出并填充字段值。但是它不适用于下面的代码。
Selenium java代码:
driver.findElement(By.xpath("//*[@id=\"loginBox\"]/div[2]/div/div/div[1]/div")).click();
Thread.sleep(2000);
Alert alert = driver.switchTo().alert();
Thread.sleep(3000);
driver.findElement(By.name("Email")).sendKeys("xxx@yyy.com");
Thread.sleep(2000);
alert.accept();HTML :
<div class="content">
<form class="ui form">
<div class="field">
<label for="Email">Email</label>
<div view="horizontal" class="ui right corner labeled input">
<div class="ui label label right corner">
<i aria-hidden="true" class="asterisk icon">
</i>
</div><input name="Email" id="Email" placeholder="Please enter email address" type="email" value="">
</div>
</div>发布于 2019-10-13 16:27:29
弹出的是HTML元素,而不是使用driver.switchTo().alert()的模态对话框.
要输入电子邮件,您必须等待元素,以及sleep错误的选择。下面的代码正在等待email的可见性,使用WebDriverWait,这是最佳实践。
此外,使用像选择器这样的//*[@id=\"loginBox\"]/div[2]/div/div/div[1]/div也不是一个好的实践。您可以找到一些关于选择器这里最佳实践的有用信息。
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
// ...
WebDriverWait wait = new WebDriverWait(driver, 10);
driver.findElement(By.xpath("//*[@id=\"loginBox\"]/div[2]/div/div/div[1]/div")).click();
WebElement email = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("Email")));
email.sendKeys("xxx@yyy.com");
email.submit();https://stackoverflow.com/questions/58364363
复制相似问题