预期条件失败:正在硒中单击元素

时间:2019-07-16 17:17:20

标签: java selenium selenium-webdriver automated-tests

需要帮助。

M试图登录Flipkart并通过硒购买第一台Iphone。

但是,我陷入了下面提到的页面

https://www.flipkart.com/apple-iphone-6 ... / p / itmen2yynt6bz3gg ...

我尝试单击“比较”复选框,16GB按钮,“立即购买”按钮和“添加到购物车”按钮,但是所有操作都无法找到错误。

任何帮助将不胜感激。

预先感谢

我已经使用了隐式等待和预期等待,但是都没有等待。

我还检查了页面中的iframe,但这也不起作用。

@Test(优先级= 3,已启用= true)   公共无效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(尝试100秒,间隔为500毫秒)     在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(本机方法)处     在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)     在

2 个答案:

答案 0 :(得分:0)

您正在做的错误是在类名中使用空格,即By.className(“ _ 2AkmmA _2Npkh4 _2kuvG8 _7UHT_c”->这是不正确的。

        driver.get(
            "https://www.flipkart.com/apple-iphone-6-space-grey-32-gb/p/itmetmh3hfhnxtcj?pid=MOBETMH3ZYNDPVVC&srno=s_1_1&otracker=search&otracker1=search&lid=LSTMOBETMH3ZYNDPVVCO3WYBD&fm=SEARCH&iid=d9dfe6a7-d4c6-48b0-b4f2-2046c0033347.MOBETMH3ZYNDPVVC.SEARCH&ppt=sp&ppn=sp&ssid=ouj8l7jf740000001563304721353&qH=43a53d285ae7d271");

    WebElement addToCard = driver.findElementByClassName("_2AkmmA");
    System.out.println(" addToCard text is " + addToCard.getText());

    WebElement buyNowButton = driver.findElementByClassName("_2kuvG8");
    System.out.println(" buyNowButton text is " + buyNowButton.getText());

我没看错文字

答案 1 :(得分:0)

我发现点击第一个搜索结果(第一个iPhone)会打开一个新标签,在这种情况下,您必须切换到新标签并执行进一步的操作。

driver.get(
        "https://www.flipkart.com/search?q=iphone&sid=tyy%2C4io&as=on&as-show=on&otracker=AS_QueryStore_OrganicAutoSuggest_0_4&otracker1=AS_QueryStore_OrganicAutoSuggest_0_4&as-pos=0&as-type=RECENT&as-backfill=on");

By firstSearchResult = By.className("_3O0U0u");
driver.findElement(firstSearchResult).click();

Thread.sleep(1000);

String currentWindow = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();
for (String window : windows) {
    if (!window.equals(currentWindow)) {
        driver.switchTo().window(window);
    }
}

// I'm performing these three steps to set pin-code, as i'm not logging-in. You
// may not have to perform these three steps as you are already logged -in.
driver.findElement(By.id("pincodeInputId")).clear();
driver.findElement(By.id("pincodeInputId")).sendKeys("500081");
driver.findElement(By.xpath("//*[@class='_2aK_gu' and text()[contains(.,'Check')]]")).click();


By button = By.xpath("//button[text()[contains(.,'BUY NOW')]]");
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(button));
driver.findElement(button).click();