在Selenium Android Webdriver测试中使用Xpath和CSS寻找元素时遇到困难[英] Having difficulty in finding Elements using Xpath and CSS in Selenium Android Webdriver Testing

本文是小编为大家收集整理的关于在Selenium Android Webdriver测试中使用Xpath和CSS寻找元素时遇到困难的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我想使用selenium webdriver自动执行Android Web应用程序,并运行一个简单的程序来打开Goog​​le页面并搜索一个术语.在找到具有名称和ID的元素,它完全运行.但是在尝试找到与XPath和CSS的元素似乎很困难.有人知道如何在selenium android webdriver中使用xpath和css路径吗?以下是我使用的示例代码:

public class TestDriver extends TestCase 
{

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


    driver.get("http://google.com");
    WebElement element = driver.findElement(By.name("q"));
       //WebElement element = driver.findElement(By.xpath("//*[@id='gbqfq']"));
       //WebElement element = driver.findElement(By.cssSelector("#gbqfq"));
    element.sendKeys("android driver");
    element.submit();
    System.out.println("Page title is: " + driver.getTitle());
    //driver.quit();
  }

}

推荐答案

我已经设法证明By.xpath和By.cssSelector能够找到可以在我们的代码中使用的元素与网页交互.由于几个实际原因,我创建了一个阴影,简化的测试页,然后提交查询以获取Google搜索引擎的搜索结果.

这里是简化的测试页面,足以展示基础知识.将此文件复制到Android设备或AVD的SDCard.使用以下命令adb push form.html /sdcard/form.html

<html>
<head>
  <title>WebDriver form test page</title>
</head>
<body>
<form name="testform" action="http://www.google.co.uk/search" method="get">
  <input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q">
  <input type="submit" value="search Google">
</form>
</body>
</html>

以及以下是您问题中的代码的修改版本:

@Test
public void testByClauses() throws Exception {
     AndroidDriver driver = new AndroidDriver();
     // driver.get("http://www.google.co.uk/");
     driver.get("file:///sdcard/form.html");
     System.out.println(driver.getPageSource());

     WebElement nameElement = driver.findElement(By.name("q"));
     WebElement xpathElement = driver.findElement(By.xpath("//*[@id='gbqfq']"));
     WebElement cssElement = driver.findElement(By.cssSelector("#gbqfq"));

     nameElement.sendKeys("android ");
     xpathElement.sendKeys("driver ");
     cssElement.sendKeys("webdriver");
     nameElement.submit();
     System.out.println("Page title is: " + driver.getTitle());
     driver.quit();
}

我碰巧使用JUnit 4作为测试跑步者(因此@Test注释)但是此代码也应该在JUnit 3中工作.

要注意:

  • 我有故意使用3个单独的webelement,它引用相同的底层html元素
  • 每个sendKeys(...)呼叫将文本附加到搜索查询
  • 技术上我们不需要提交搜索查询来演示By(...)条文的工作,但是很高兴看到代码执行并获得一些匹配的搜索结果.
  • 我评论了加载Google搜索的主页的请求,但在我的答案中留下了它,以便您可以轻松地看到如何使用Google网站而不是静态本地HTML页面.
  • 我用局部连接的nexus 4电话,超过USB进行了测试,并使用了AndroidDriver()的本地构建实例.

其他推荐答案

对我有好处.

//Author: Oleksandr Knyga
function xPathToCss(xpath) {
    return xpath
        .replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
        .replace(/\/{2}/g, '')
        .replace(/\/+/g, ' > ')
        .replace(/@/g, '')
        .replace(/\[(\d+)\]/g, ':eq($1)')
        .replace(/^\s+/, '');
}

本文地址:https://www.itbaoku.cn/post/102585.html

问题描述

I want to automate android web apps using Selenium WebDriver and i've run a simple program to open the google page and search a term . While finding the element with the name and id it runs perfectly. But while trying to find the element with the Xpath and Css seems difficult. Does anybody know how to use xpath and css path in selenium android webdriver? Here is the sample code i've used:

public class TestDriver extends TestCase 
{

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


    driver.get("http://google.com");
    WebElement element = driver.findElement(By.name("q"));
       //WebElement element = driver.findElement(By.xpath("//*[@id='gbqfq']"));
       //WebElement element = driver.findElement(By.cssSelector("#gbqfq"));
    element.sendKeys("android driver");
    element.submit();
    System.out.println("Page title is: " + driver.getTitle());
    //driver.quit();
  }

}

推荐答案

I have managed to prove that By.xpath and By.cssSelector are able to find elements which can then use in our code to interact with the web page. For a couple of practical reasons I created a shadow, simplified test page which then submits a query to obtain search results from Google's search engine.

Here is the simplified test page, enough to demonstrate the basics. Copy this file to the sdcard of your android device or AVD e.g. using the following command adb push form.html /sdcard/form.html

<html>
<head>
  <title>WebDriver form test page</title>
</head>
<body>
<form name="testform" action="http://www.google.co.uk/search" method="get">
  <input id="gbqfq" class="gbqfif" type="text" value="" autocomplete="off" name="q">
  <input type="submit" value="search Google">
</form>
</body>
</html>

And here's the modified version of the code in your question:

@Test
public void testByClauses() throws Exception {
     AndroidDriver driver = new AndroidDriver();
     // driver.get("http://www.google.co.uk/");
     driver.get("file:///sdcard/form.html");
     System.out.println(driver.getPageSource());

     WebElement nameElement = driver.findElement(By.name("q"));
     WebElement xpathElement = driver.findElement(By.xpath("//*[@id='gbqfq']"));
     WebElement cssElement = driver.findElement(By.cssSelector("#gbqfq"));

     nameElement.sendKeys("android ");
     xpathElement.sendKeys("driver ");
     cssElement.sendKeys("webdriver");
     nameElement.submit();
     System.out.println("Page title is: " + driver.getTitle());
     driver.quit();
}

I happen to be using JUnit 4 as the test runner (hence the @Test annotation) however this code should also work in JUnit 3.

Points to note:

  • I have deliberately used 3 separate WebElements which refer to the same underlying HTML element
  • Each sendKeys(...) call appends text to the search query
  • Technically we don't need to submit the search query to demonstrate the By(...) clauses work, however it's good to see the code execute and get some matching search results.
  • I commented out the request to load Google search's homepage, but left it in my answer so you can easily see how to use the Google site instead of the static local HTML page.
  • I ran the test with a locally connected Nexus 4 phone, over USB, and used a locally built instance of the AndroidDriver().

其他推荐答案

Works good for me.

//Author: Oleksandr Knyga
function xPathToCss(xpath) {
    return xpath
        .replace(/\[(\d+?)\]/g, function(s,m1){ return '['+(m1-1)+']'; })
        .replace(/\/{2}/g, '')
        .replace(/\/+/g, ' > ')
        .replace(/@/g, '')
        .replace(/\[(\d+)\]/g, ':eq($1)')
        .replace(/^\s+/, '');
}