0

我正在使用 webdriver for android 来自动化网站测试。我已经安装了网络驱动程序并打开了谷歌页面。但它没有接受我输入的搜索项。这里是“奶酪”。我提到了下面的代码。请提出解决方案。谢谢

public class AndroidSeleniumActivity extends TestCase 
{
    /** Called when the activity is first created. */

    public void testGoogle() throws Exception 
{
        AndroidDriver 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.xpath("html/body/div[2]/div[1]/div[1]/div[2]/div[2]/div/form/fieldset [2]/div/div/div/table/tbody/tr/td[2]/div/input[3]"));

        // 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

        // Remember to close the WebDriver connection to free up resources on the
        // Android device (or emulator). Pause for 30 seconds first so we can see
        // the results of the search.
       // Thread.sleep(30000L);
        driver.quit();
      }

}
4

1 回答 1

0

使用 id 或 name 来查找元素(搜索文本字段)而不是使用

XPATH: WebElement element =  driver.findElement(By.xpath("html/body/div[2]/div[1]/div[1]/div[2]/div[2]/div/form/fieldset [2]/div/div/div/table/tbody/tr/td[2]/div/input[3]"));

使用它,它将起作用:

WebElement element =  driver.findElement(By.name("q"));
于 2012-06-11T05:40:48.723 回答