1

我正在自动化一个用于购物的 Web 应用程序。在特定页面中,我必须通过单击提交按钮来提交。我在 selenium web 驱动程序中编写代码以实现同样的事情。单击了该按钮,但它从未导航到下一页,也没有抛出任何异常,我可以看到测试成功运行。

package org.karmaloop.testcase;

import java.io.File;
import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.karmaloop.configuration.Testconfiguration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

@RunWith(BlockJUnit4ClassRunner.class)
public class testcase1 {

    private static ChromeDriverService srv;
    private WebDriver driver;

    @BeforeClass
    public static void StartServer() throws IOException {
        // Below file path to Chrome browser should be changed accordingly
        srv = new ChromeDriverService.Builder()
                .usingDriverExecutable(
                        new File("D:\\chromedriver\\chromedriver.exe"))
                .usingAnyFreePort().build();
        srv.start();
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
    }

    @Before()
    public void setUp() throws Exception {

        driver = new RemoteWebDriver(srv.getUrl(), DesiredCapabilities.chrome());
    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void test() throws Exception {


        Normal_Checkout();

    }

    private void Normal_Checkout() throws Exception {

        //To get commented  

          driver.get("https://m.karmaloop.com/product/The-Superstar-80s-GRF-Sneaker-in-Wheat-Black-Chalk/384883");
          driver.findElement(By.cssSelector(Testconfiguration.size_dropdown)).click();
          System.out.println("success");
          Thread.sleep(4000);
          driver.findElement(By.xpath(Testconfiguration.select_size)).click();
          Thread.sleep(4000);
          driver.findElement(By.xpath(Testconfiguration.addtocart_button)).click();
          Thread.sleep(7000);


          driver.findElement(By.xpath(Testconfiguration.pcheckout_button)).click();
          Thread.sleep(5000);

            // To get commented   

          driver.findElement(By.xpath(Testconfiguration.checkout_logintxtbox)).sendKeys(Testconfiguration.checkout_login_username);
          Thread.sleep(5000);
          driver.findElement(By.xpath(Testconfiguration.checkout_passwordtxtbox)).sendKeys(Testconfiguration.checkout_login_password);
          Thread.sleep(5000);
          driver.findElement(By.xpath(Testconfiguration.checkout_loginbtn)).click();
          Thread.sleep(10000);
          System.out.println("Passed before checkout");
          driver.findElement(By.xpath(Testconfiguration.submit_button));
          Thread.sleep(20000);
          System.out.println("submit clicked");
          }}

==================================================== =================================我已经使用xpath来提交按钮被点击。谁能帮我解决这个问题。

4

3 回答 3

0

我以前在使用 selenium 时遇到过这个问题,虽然我没有解决方案,但我确实有一个在大多数情况下都有效的解决方法。

单击提交按钮后,如果您尝试与下一页上的元素交互,Selenium 应该通过 NoSuchElementException。抓住这个并尝试重新单击提交按钮(因为您可以假设页面没有加载)。十分之九这对我有用。

哦,还有一个提示,您可能会觉得有用,而不是每次加载页面后手动等待五秒钟,而是使用implicitlyWait。browser.implicitlyWait(30) 将让浏览器在抛出 NoSuchElementException 之前等待页面加载最多 30 秒。

希望这可以帮助。祝你好运。

编辑:我可能弄错了,我相信它建议您不要使用 XPath 作为您的选择器,除非您绝对必须这样做,因为它比其他选择(如 ID)慢得多,因为它必须遍历 XPath。

于 2013-08-08T21:54:51.107 回答
0

您可以等到页面加载完毕。

例子:

//wait until page is loaded
String title = "Should have the title of the page that should be loaded";
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.titleIs(title));

此代码将等待 60 秒,直到页面标题与作为输入传递的页面标题匹配。

于 2014-06-21T02:04:39.907 回答
0

我遇到了一个问题,它正在单击一个元素,但它没有重定向到下一页,也没有出现异常。我尝试了 Actions 类、Javascript 执行器、Robot 类和所有等待语句,但没有得到结果。这个解决方案对我有用。

try {
      //Next page element
    driver.findElement(By.xpath("//div[@class='web']/label/span[contains(text(), 'Congratulations!')]")).click();
        } catch (Exception e) {
    //current page element which is not redirecting after performing click operation
            driver.findElement(By.xpath("//div[@class='form-main-container']/button")).click();
        }

您必须尝试与下一页上的元素进行交互,并将 XPath 用于 try 块中的元素,并将 XPath 用于未重定向到 catch 块中的下一页的元素。

于 2020-02-04T13:26:53.353 回答