Java 单击submit按钮时,用户不会导航到SeleniumWebDriver中的下一页

Java 单击submit按钮时,用户不会导航到SeleniumWebDriver中的下一页,java,xpath,selenium,selenium-webdriver,Java,Xpath,Selenium,Selenium Webdriver,我正在为购物自动化一个Web应用程序。 在特定页面中,我必须通过单击submit按钮进行提交。我在SeleniumWeb驱动程序中编写代码,以实现同样的效果。 按钮被点击,但它从未导航到下一页,也没有引发任何异常,我可以看到测试成功运行 package org.karmaloop.testcase; import java.io.File; import java.io.IOException; import org.junit.After; import org.junit.AfterCl

我正在为购物自动化一个Web应用程序。 在特定页面中,我必须通过单击submit按钮进行提交。我在SeleniumWeb驱动程序中编写代码,以实现同样的效果。 按钮被点击,但它从未导航到下一页,也没有引发任何异常,我可以看到测试成功运行

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为submit按钮单击提供了支持。有人能帮我解决这个问题吗。

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

单击submit按钮后,如果尝试在下一页上与元素交互,则Selenium应通过NoTouchElementException进行交互。捕获此消息并尝试重新单击submit按钮(因为您可以假定页面未加载)。十有八九这对我有用

哦,还有一个你可能会发现很有用的提示,与其每次加载页面后手动等待五秒钟,不如使用。隐式wait(30)将让浏览器等待30秒,等待页面加载,然后抛出NosTouchElementException

希望这有帮助。祝你好运


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

您可以等待页面加载

例如:

//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秒,直到页面标题与作为输入传递的标题匹配。

我遇到了一个问题,它单击了一个元素,但没有重定向到下一页,也没有收到异常。我尝试了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();
        }

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

如果您能提供相关的HTML片段并显示您在
Testconfiguration.提交按钮
调用。谢谢。另外,请帮自己一个大忙:阅读显式等待,停止使用Thread.sleep()。由于所有这些延迟,您要花费大量的时间(在一系列测试中花费数小时)。即使您只是为了一个原型而删掉一些东西,也要避免使用Thread.sleep()。您在selenium IDE中也尝试过同样的方法吗?其他类型的定位器(如CSS)呢?通常,如果内容是动态的,CSS往往比XPath更稳定。