Selenium - 任何使用 AndroidDriver 的人

发布于 2024-11-16 03:35:58 字数 5732 浏览 2 评论 0原文

我在模拟器中启动了 WebDriver 并连接了 eclipe 中的代码。它正在加载并显示“找不到网页”。以下是 google.com 的示例代码片段:

public class OneTest extends TestCase {

  public void testGoogle() throws Exception {
    AndroidDriver driver = new AndroidDriver();

// And now use this to visit Google
driver.get("http://www.google.com");



/*WebDriverWebView.enablePlatformNotifications();
    final WebDriverWebView newView = new WebDriverWebView(this);
    newView.setHttpAuthUsernamePassword("10.152.80.42", "proxy ip",
    "userid", "password"); 
  */
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
//element.sendKeys("Cheese!");

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();

  }
}

抛出以下错误?

org.openqa.selenium.NoSuchElementException: Element not found.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
System info: os.name: 'Linux', os.arch: 'armv5tejl', os.version: '2.6.29-00261-g0097074', java.version: '0'
Driver info: driver.version: EventFiringWebDriver
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_25'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:192)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:241)
    at org.openqa.selenium.By$4.findElement(By.java:148)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:184)
    at OneTest.testGoogle(OneTest.java:26)
    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 junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    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)
Caused by: org.openqa.selenium.NoSuchElementException: Element not found.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
System info: os.name: 'Linux', os.arch: 'armv5tejl', os.version: '2.6.29-00261-g0097074', java.version: '0'
Driver info: driver.version: EventFiringWebDriver
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_25'
Driver info: driver.version: EventFiringWebDriver
    at org.openqa.selenium.android.JavascriptDomAccessor.getFirstElement(JavascriptDomAccessor.java:838)
    at org.openqa.selenium.android.JavascriptDomAccessor.getElementByName(JavascriptDomAccessor.java:170)
    at org.openqa.selenium.android.AndroidWebElement.findElementByName(AndroidWebElement.java:218)
    at org.openqa.selenium.android.AndroidDriver.findElementByName(AndroidDriver.java:160)
    at org.openqa.selenium.By$ByName.findElement(By.java:272)
    at org.openqa.selenium.android.AndroidDriver.findElement(AndroidDriver.java:119)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:77)
    at $Proxy1.findElement(Native Method)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:153)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:41)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    at java.lang.Thread.run(Thread.java:1096)

I started WebDriver in emulator and connected the code from eclipe. It is loading and saying "webpage not found" . The following is the snippet of sample code for google.com:

public class OneTest extends TestCase {

  public void testGoogle() throws Exception {
    AndroidDriver driver = new AndroidDriver();

// And now use this to visit Google
driver.get("http://www.google.com");



/*WebDriverWebView.enablePlatformNotifications();
    final WebDriverWebView newView = new WebDriverWebView(this);
    newView.setHttpAuthUsernamePassword("10.152.80.42", "proxy ip",
    "userid", "password"); 
  */
driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS);
// Find the text input element by its name
WebElement element = driver.findElement(By.name("q"));

// Enter something to search for
//element.sendKeys("Cheese!");

// Now submit the form. WebDriver will find the form for us from the element
element.submit();

// Check the title of the page
System.out.println("Page title is: " + driver.getTitle());
driver.quit();

  }
}

following error is thrown?

org.openqa.selenium.NoSuchElementException: Element not found.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
System info: os.name: 'Linux', os.arch: 'armv5tejl', os.version: '2.6.29-00261-g0097074', java.version: '0'
Driver info: driver.version: EventFiringWebDriver
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_25'
Driver info: driver.version: RemoteWebDriver
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:131)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:105)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:409)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:192)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByName(RemoteWebDriver.java:241)
    at org.openqa.selenium.By$4.findElement(By.java:148)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:184)
    at OneTest.testGoogle(OneTest.java:26)
    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 junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    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)
Caused by: org.openqa.selenium.NoSuchElementException: Element not found.
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
System info: os.name: 'Linux', os.arch: 'armv5tejl', os.version: '2.6.29-00261-g0097074', java.version: '0'
Driver info: driver.version: EventFiringWebDriver
System info: os.name: 'Windows XP', os.arch: 'x86', os.version: '5.1', java.version: '1.6.0_25'
Driver info: driver.version: EventFiringWebDriver
    at org.openqa.selenium.android.JavascriptDomAccessor.getFirstElement(JavascriptDomAccessor.java:838)
    at org.openqa.selenium.android.JavascriptDomAccessor.getElementByName(JavascriptDomAccessor.java:170)
    at org.openqa.selenium.android.AndroidWebElement.findElementByName(AndroidWebElement.java:218)
    at org.openqa.selenium.android.AndroidDriver.findElementByName(AndroidDriver.java:160)
    at org.openqa.selenium.By$ByName.findElement(By.java:272)
    at org.openqa.selenium.android.AndroidDriver.findElement(AndroidDriver.java:119)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:521)
    at org.openqa.selenium.support.events.EventFiringWebDriver$2.invoke(EventFiringWebDriver.java:77)
    at $Proxy1.findElement(Native Method)
    at org.openqa.selenium.support.events.EventFiringWebDriver.findElement(EventFiringWebDriver.java:153)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:41)
    at org.openqa.selenium.remote.server.handler.FindElement.call(FindElement.java:1)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
    at java.lang.Thread.run(Thread.java:1096)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-11-23 03:35:58

尝试

AndroidDriver driver = new AndroidDriver();

用这一行替换以下行。

WebDriver driver = new AndroidDriver();

这对我有用。

Try replacing the following line

AndroidDriver driver = new AndroidDriver();

with this one.

WebDriver driver = new AndroidDriver();

This has worked for me.

柠檬色的秋千 2024-11-23 03:35:58

下面的代码应该会有帮助:

import junit.framework.TestCase;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;

public class OneTest extends TestCase {

  public void testGoogle() throws Exception {
    WebDriver driver = new AndroidDriver();

    // And now use this to visit Google
    driver.get("http://www.google.com");

    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());
    driver.quit();
  }
}

The following code should be helpful:

import junit.framework.TestCase;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.android.AndroidDriver;

public class OneTest extends TestCase {

  public void testGoogle() throws Exception {
    WebDriver driver = new AndroidDriver();

    // And now use this to visit Google
    driver.get("http://www.google.com");

    // Find the text input element by its name
    WebElement element = driver.findElement(By.name("q"));

    // Enter something to search for
    element.sendKeys("Cheese!");

    // Now submit the form. WebDriver will find the form for us from the element
    element.submit();

    // Check the title of the page
    System.out.println("Page title is: " + driver.getTitle());
    driver.quit();
  }
}
蓝颜夕 2024-11-23 03:35:58

我发现您应该使用适当的 .apk 文件。我觉得 ICS 比任何其他版本都更好。

另外,在运行之前始终进行端口转发。

adb forward tcp:8080 tcp:8080

What I have seen is that you should use the appropriate .apk file. I feel the ICS is more than any other version.

Also before running always do the port forwarding.

adb forward tcp:8080 tcp:8080
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文