3

我用

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

我尝试使用

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

两者都没有给我结果。

我收到如下错误:-

元素链接 =“登录”未找到错误。

代码:

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

我之前也遇到过同样的问题,在 selenium 中经常出现“找不到元素”问题。这个问题的可能原因如下:

  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);
         }
     }
 }
于 2012-05-20T13:57:16.700 回答
1

<a>元素是动态创建的(并且以一种糟糕的方式 - 它会在很长一段时间后出现)并且 Selenium 找不到它。如果你查看页面的源代码,你只能看到

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

原因是在其中一个创建登录链接的 js 文件中,如下所示:

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

Selenium 可以找到动态创建的元素,但只能在创建之后,而不是之前。

出现的第一个解决方案selenium.click("id=global-signin")不起作用,因为找到并单击了元素,但尚未包含实际链接(稍后由 js 创建)。


解决方案是等待元素出现:

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

您甚至可以编写自己的click(String)方法(以及其他方法,可能是通过代码重复的简单方法,或者通过命令模式的更难但更好的方法)将其包含在每次搜索中。

Selenium 2 (WebDriver)中,通过隐式等待会容易得多:

// 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 回答
1

这很难——我遇到的问题在第二页——除非我先选择目标定位器,否则 Selenium 无法将任何数据输入到表单中。这是我的解决方案:

import java.util.concurrent.TimeUnit;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class LoginTest {

    private static final String USERNAME = "username";
    private static final String PASSWORD = "password";
    private static final int PAUSE = 10;

    @Test
    public void testWebsiteTwo() throws Exception {
        WebDriver driver = new ChromeDriver();
        driver.manage().timeouts().implicitlyWait(PAUSE, TimeUnit.SECONDS);
        driver.get("http://www.washingtonpost.com/");
        WebElement signin = driver.findElement(By
                .cssSelector("li#global-signin a"));
        signin.click();

        // this line is crucial so that Selenium can enter the username / password

        WebDriver.TargetLocator locator = driver.switchTo();
        locator.frame(0);
        WebElement name = driver.findElement(By.cssSelector("input#username"));
        name.sendKeys(USERNAME);
        WebElement password = driver.findElement(By.cssSelector("input#password"));
        password.sendKeys(PASSWORD);
        WebElement form = driver.findElement(By.cssSelector("form[name=form]"));
        form.submit();
        Thread.sleep(5000);
        driver.close();
    }

}
于 2013-08-30T01:47:02.830 回答