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 は NoSuchElementException を通過する必要があります。これをキャッチして、送信ボタンの再クリックを試みます (ページが読み込まれなかったと想定できるため)。10回のうち9回はこれでうまくいきます。

ページをロードするたびに手動で 5 秒間待機する代わりに、暗黙的にWait を使用してください。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 に答える