3

私は

selenium.click("link=Sign In");

そして、私は

selenium.click(".//*[@id='global-signin']/a");

どちらも私に結果をもたらしませんでした。

以下のようなエラーが表示されます:-

Element Link="Sign In" エラーが見つかりません。

コード:

package package1_IdentifyPageOpened;


import com.thoughtworks.selenium.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
//import java.util.regex.Pattern;

public class PublicClass3 {

    /**
     * @param args
     */
    Selenium selenium;

    @Before
    public void setUp() throws Exception {
        selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.washingtonpost.com/");
        selenium.start();
    }

    @Test
    public void testTt4() throws Exception {
        selenium.open("/");
        selenium.click("link=Sign In");
        selenium.type("name=MemberName", "mcXXX@gmail.com");
        selenium.type("name=Password", "PPP@123");
        selenium.click("name=submit");
        selenium.waitForPageToLoad("30000");

    }

    @After
    public void tearDown() throws Exception {
        selenium.stop();
    }


    public static void main(String[] args) {
        // TODO Auto-generated method stub

    }

}

失敗のトレース:

 com.thoughtworks.selenium.SeleniumException: ERROR: Element link=Sign
 In not found   at
 com.thoughtworks.selenium.HttpCommandProcessor.throwAssertionFailureExceptionOrError(HttpCommandProcessor.java:112)
    at
 com.thoughtworks.selenium.HttpCommandProcessor.doCommand(HttpCommandProcessor.java:106)
    at
 com.thoughtworks.selenium.DefaultSelenium.click(DefaultSelenium.java:193)
    at
 package1_IdentifyPageOpened.PublicClass3.testTt4(PublicClass3.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)  at
 sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)    at
 sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)    at
 java.lang.reflect.Method.invoke(Unknown Source)    at
 org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at
 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at
 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at
 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at
 org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at
 org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:30)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)    at
 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at
 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)  at
 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)    at
 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)  at
 org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)    at
 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)   at
 org.junit.runners.ParentRunner.run(ParentRunner.java:300)  at
 org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at
 org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at
 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at
 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at
 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at
 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
4

3 に答える 3

3

私も以前に同じ問題に遭遇し、「要素が見つかりません」という問題がセレンで頻繁に発生しています。この問題の考えられる原因は次のとおりです。

  1. 「クリック要素」を実行すると、要素がページに表示されていない場合、おそらく読み込みの過程で、セレンは例外をスローします:要素が見つかりません。

  2. 要素の ID が間違っています。スペル ミスか、セレンが ID の方法をサポートしていない可能性があります。

テストしたウェブサイトhttp://www.washingtonpost.comにアクセスしました

Java としてエクスポートすると、常に記録された Test-CaseSeleniumIDEが機能しません。

これが私のコードです:

 public class Website extends SeleneseTestBase {
     @BeforeMethod
     public void setUp() throws Exception {
         selenium = new DefaultSelenium("localhost", 4444, "*chrome", "http://www.washingtonpost.com/");
         selenium.start();
     }

     @Test
     public void testWebsite() throws Exception {
         selenium.open("/");
         waitFor("link=Sign In");
         waitSecomd(3);
         selenium.click("link=Sign In");
         selenium.waitForPageToLoad("60000");
         selenium.type("name=MemberName", "adasf@gmail.com");
         selenium.type("name=Password", "adfasd");
         selenium.click("name=submit");
         selenium.waitForPageToLoad("60000");
     }

     @AfterMethod
     public void tearDown() throws Exception {
         selenium.stop();
     }
     public void waitFor(String locator) throws Exception {
         for (int second = 0;; second++) {
             if (second >= 60)
                 fail("timeout");
             try {
                 if (selenium.isVisible(locator))
                     break;
             } catch (Exception e) {}
             Thread.sleep(1000);
         }
     }


     public void waitSecomd(int n) throws Exception {
         for (int second = 0;; second++) {
             if (second >= 60)
                 fail("timeout");
             try {
                 if (second > n - 1)
                     break;
             } catch (Exception e) {}
             Thread.sleep(1000);
         }
     }
 }
于 2012-05-20T13:57:16.700 に答える
1

The <a> element is created dynamically (and in a bad way - it shows up after a long time) and Selenium can't find it. If you look into the source code of the page, you can only see

<div id="utility-wrapper" data-tracking-type="utility">
    <ul id="utility-links" class="inline-list">  
        <li id="global-signin" style="min-width:32px;"></li>

The cause is in one of the js files where the Sign In link is created as follows:

I.innerHTML = '<a href="' + D + "...a loong piece of URL..." + H + '">Sign In</a>';

Selenium can find a dynamically created element, but only after it is created, not before.

The first solution that comes to head, selenium.click("id=global-signin") doesn't work because the element is found and clicked, but doesn't yet contain the actual link (which is created by js some time later).


The solution is to wait for the element to show up:

long targetTime = System.currentTimeMillis() + 10000;  // 10 seconds from now
boolean found = false;
while (System.currentTimeMillis() < targetTime && !found) {
    if (selenium.isElementPresent("link=Sign In")) {
        selenium.click("link=Sign In");
        found = true;
    }
}
if (!found) {
    throw new SeleniumException("Element not found!");
}

you can even write your own click(String) method (and others too, it's possible either the easy way via code repetition, or the harder but nicer way via the Command pattern) that incorporates this for use in every search.

In Selenium 2 (WebDriver) it would be much easier to do via Implicit wait:

// open Firefox
WebDriver driver = new FirefoxDriver();
// set the Implicit wait to 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://www.washingtonpost.com/");
driver.findElement(By.linkText("Sign In")).click();
于 2012-05-19T18:48:21.163 に答える