java - 与 Java 驱动程序一起使用时,如何在 Selenium 中触发链接的单击事件?

标签 java eclipse selenium selenium-rc

我用过

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

我尝试使用

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

两者都没有给我结果。

我收到如下错误:-

Element Link="Sign In" not found error.

代码:

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)

最佳答案

我之前也遇到过同样的问题,在selenium中经常出现'Element not found'问题,可能的原因如下:

  1. 当你执行“点击元素”时,但是元素没有显示在页面中,可能是在加载过程中,selenium会抛出一个异常:找不到元素。

  2. 元素标识错误,可能是拼写错误或者selenium不支持标识方式。

我访问了您测试的网站,http://www.washingtonpost.com

当您导出为 Java 时,SeleniumIDE 录制的测试用例总是不起作用。

这是我的代码:

 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);
         }
     }
 }

关于java - 与 Java 驱动程序一起使用时,如何在 Selenium 中触发链接的单击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10651616/

相关文章:

java - 正在添加空默认 XML 命名空间 xmlns =""属性?

java - 如何使用注解通过改造 2 在 android 中解析动态 json

java - 如何使用今天的日期传入文件名值属性

java - 我的 Android 应用程序运行非常缓慢

java - 使用 Ubuntu 13.04 桌面在 Nexus 7 上运行 Eclipse

selenium - 我们什么时候在 selenium chrome 选项中使用 setExperimentalOptions?

java - jaxb 解码 xml 数据的默认规则是什么?

java - 如何设置 eclipse/webapp maven?

java - Selenium 网络驱动程序 : Element Not Visible Exception

php - 如何在 PHP 中重用 selenium session ?