【问题标题】:Not able to read the model value using selenium无法使用 selenium 读取模型值
【发布时间】: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();
     }
}

【问题讨论】:

标签: java selenium selenium-webdriver


【解决方案1】:

模态窗口是模拟的,而不是实际的浏览器窗口。因此,模态窗口内元素的 HTML 代码在同一页面中。使用 DevTool 可以简单地验证这一点。

模态窗口的所有内容都来自一个ID为p_layer的DIV节点,所以你可以找到内部元素从#p_layer开始

【讨论】:

  • 如果你看到上面的代码,我提到了#p_layer。但我仍然无法访问内部元素。
  • 代码是否会在这一行失败:wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("p_layer")));
  • wait.until(ExpectedConditions.visibilityOfElementLocated(By.‌​id("p_layer"))); .当我尝试单击按钮 driver.findElement(By.xpath("//a[@data-qa='goToBasket']")).click();那么它就无法找到并走得更远。
  • 你能上传错误堆栈吗?我尝试了 xpath: //a[@data-qa='goToBasket'],它将匹配两个元素,但第二个是你想要点击的。我猜错误应该说元素不可见,因为第一个匹配的元素不可见,但您的代码将点击第一个。请尝试您的代码 driver.findElement(By.xpath("//a[@data-qa='goToBasket'][2]")).c‌​lick();
  • 抱歉,我无法访问该元素。错误:原因:org.openqa.selenium.NoSuchElementException:无法使用 By.xpath 定位元素://a[@data-qa='goToBasket'][2] 有关此错误的文档,请访问:seleniumhq.org/exceptions/no_such_element.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-02
  • 2018-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多