【发布时间】:2019-11-25 11:55:28
【问题描述】:
需要帮助。
M 试图登录 Flipkart 并通过 selenium 购买第一部 Iphone。
但是,我卡在下面提到的页面中
https://www.flipkart.com/apple-iphone-6…/p/itmen2yynt6bz3gg…
我尝试点击比较复选框、16GB 按钮、立即购买按钮和添加到购物车按钮,但所有操作都出现无法定位错误。
任何帮助将不胜感激。
提前致谢
我使用过隐式等待和预期等待,但都没有等待。
我还检查了页面中的 iframe,但这也不起作用。
@Test(优先级 = 3,启用 = true) public void inPage() 抛出 InterruptedException {
WebDriverWait wait = new WebDriverWait(driver, 100);
wait.until(ExpectedConditions.elementToBeClickable(By.className("_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c"))).click();
}
通过:登录 通过:搜索 失败:inPage org.openqa.selenium.TimeoutException:预期条件失败:等待元素可点击:By.className:_2AkmmA _2Npkh4 _2kuvG8 _7UHT_c(以 500 毫秒间隔尝试 100 秒) 在 org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95) 在 org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272) 在 First.inPage(First.java:94) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(未知来源) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在 java.lang.reflect.Method.invoke(未知来源) 在 org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124) 在 org.testng.internal.Invoker.invokeMethod(Invoker.java:583) 在 org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719) 在 org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989) 在
【问题讨论】:
-
与其使用通用定位器类型(此处为类),不如尝试使用更有效的定位器(如 id、name、xpath 等)。确保您尝试点击的元素在页面上可见且已启用。
-
感谢您的回复@AppuMistr。我也使用了 Xpath 和 CSS,但无济于事。为了检查它们的可见性,我使用了下面提到的代码,但 M 得到“预期条件失败:等待元素可点击”错误
-
public void inPage() throws InterruptedException { WebDriverWait wait = new WebDriverWait(driver, 100); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"container\"]/div/div[3]/div[2]/div[1]/div[1]/div) [2]/div/ul/li[2]/form/button"))); boolean enabled = driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[3]/div[2]/div[1]/div[1]/div[ 2]/div/ul/li[2]/form/button")).isEnabled(); boolean visible = driver.findElement(By.xpath("//*[@id=\"container\"]/div/div[3]/div[2]/div[1]/div[1]/div[ 2]/div/ul/li[2]/form/button")).isDisplayed(); }
-
点击第一个搜索结果(第一个 iPhone 链接)是否会将您带到不同的选项卡?如果是这样,请切换到新选项卡并执行进一步的操作。
-
非常感谢这个建议,它对我有用。上帝保佑你。
标签: java selenium selenium-webdriver automated-tests