场景是我们必须登录到这个网站:http://way2automation.com,然后转到警报页面,然后单击简单警报中的按钮。此按钮没有任何ID。只有一个简单的Java脚本功能。我们怎么办呢?
package sampletest;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class alertspractice {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.setProperty("webdriver.chrome.driver","F:\\drivers for browsers\\chrome driver\\chromedriver.exe");
ChromeDriver driver=new ChromeDriver();
driver.get("http://way2automation.com/way2auto_jquery/index.php");
driver.manage().window().maximize();
WebElement name=driver.findElement(By.name("name"));
name.sendKeys("jayath");
WebElement Phone =driver.findElement(By.name("phone"));
Phone.sendKeys("8790141375");;
WebElement email=driver.findElement(By.name("email"));
email.sendKeys("a@yopmail.com");
WebElement city =driver.findElement(By.name("city"));
city.sendKeys("visakhapatnam");
WebDriverWait wait=new WebDriverWait(driver,40);
wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath(".//[@id='load_form']/fieldset[6]/input")));
WebElement username =driver.findElement(By.xpath(".//*[@id='load_form']/fieldset[6]/input"));
username.sendKeys("jayath1234");
WebElement password =driver.findElement(By.xpath(".//*[@id='load_form']/fieldset[7]/input"));
password.sendKeys("jayath234");
WebElement submit1 =driver.findElement(By.xpath(".//*[@id='load_form']/div/div[2]/input"));
submit1.submit();
WebDriverWait wait2=new WebDriverWait(driver,40);
wait2.until(ExpectedConditions.presenceOfElementLocated(By.linkText("Alert")));
WebElement alert123=driver.findElement(By.linkText("Alert"));
alert123.click();
wait2.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//html/body/button")));
WebElement getalert=driver.findElement(By.xpath("//html/body/button"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
}
}
答案 0 :(得分:0)
最后,在挖掘之后,必须在您正在谈论的代码中获得直接链接以进入警报部分。该页面上似乎有两个按钮,一个隐藏在选项卡后面,输入警报。我会假设它是第一个按钮,但输入警报也不难改变它。
//button[contains(text(), 'Click the button to display an alert box:')]
答案 1 :(得分:0)
WebDriverWait wait2=new WebDriverWait(driver,40);
wait2.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//button[contains(text(), 'Click the button to display an alert box:')]")));
WebElement alert123=driver.findElement(By.xpath("//button[contains(text(), 'Click the button to display an alert box:')]"));
alert123.click();