【发布时间】:2017-12-29 00:23:09
【问题描述】:
我是 Selenium 网络驱动程序的新手。所以我正在尝试使用不同的网站做一些示例。在这种模式下,我尝试使用 OTTO 网站并尝试将商品添加到购物车。问题来了,在这个网站上,在将商品添加到购物车之前有一个模式窗口。
我的问题是如何进入模态窗口并访问其中的元素?下面是我试过的代码。Click here to view the image for reference
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class AutomationTesting {
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver","/Users/xxx/eclipse-workspace/seleniumTesting/lib/geckoDriver/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.get("https://www.google.de");
driver.findElement(By.id("lst-ib")).sendKeys("Pepe Jeans");
driver.findElement(By.id("lst-ib")).sendKeys(Keys.ENTER);
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.partialLinkText("Pepe Jeans Online-Shop | OTTO")));
driver.findElement(By.partialLinkText("Pepe Jeans Online-Shop | OTTO")).click();
driver.findElement(By.linkText("Pepe Jeans Chiffonkleid »MARY«, im modischen Leo-Muster")).click();
driver.findElement(By.xpath("//ul//li[@data-variationid='594838149']")).click();
driver.findElement(By.id("addToBasket")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("p_layer")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("or_itemAdded__itemInfo")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//a[@data-qa='goToBasket']")));
driver.findElement(By.xpath("//a[@data-qa='goToBasket']")).click();
}
}
【问题讨论】:
-
请阅读为什么是
screenshot of HTML or code or error is a bad idea。考虑使用基于格式化文本的 HTML 和代码试验来更新问题。
标签: java selenium selenium-webdriver