在 selenium2 中定位元素时出错

发布于 2024-12-04 04:39:01 字数 2078 浏览 3 评论 0原文

我正在使用 selenium2.0 和 testNG。当使用 XPATH 或 CSS 元素来定位其时,会显示错误“无法定位该元素”。 我用Java编程为 下面:

public class mytest {

    public static WebDriver driver;
    public Alert alert;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        driver=new FirefoxDriver();

    driver.get("http://localhost:4503/xyz.html");

    }

    public static void clickButton(WebDriver driver, String identifyBy, String locator){
        if (identifyBy.equalsIgnoreCase("xpath")){
            driver.findElement(By.xpath(locator)).click();
        }else if (identifyBy.equalsIgnoreCase("id")){
            driver.findElement(By.id(locator)).click();
        }else if (identifyBy.equalsIgnoreCase("name")){
            driver.findElement(By.name(locator)).click();
        }

    }

    public static void typeinEditbox(WebDriver driver, String identifyBy, String locator, String valuetoType){
        if (identifyBy.equalsIgnoreCase("xpath")){
            driver.findElement(By.xpath(locator)).sendKeys(valuetoType);
        }else if (identifyBy.equalsIgnoreCase("id")){
            driver.findElement(By.id(locator)).sendKeys(valuetoType);
        }else if (identifyBy.equalsIgnoreCase("name")){
            driver.findElement(By.name(locator)).sendKeys(valuetoType);
        }

    }


    public static void openApplication(WebDriver driver, String url) {
        driver.get(url);
    }






    @Test
    public void testAcrolinxApplication() throws InterruptedException {
        openApplication(driver,"http://xyz.com");
        typeinEditbox(driver,"name","p_user","xxx");
        typeinEditbox(driver,"name","p_pas","yyy");
        clickButton(driver,"id","input-submit");

/*Up to this its working fine …..

At below line this throws error could not locate the xpath element "//a[@id='cq-gen100']/em/span/span" BUT THIS IS WOKING FINE IN Selenium1.0 api that is with 
selenium.click("//a[@id='cq-gen100']/em/span/span"); */

driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span/span")).click();




    }

}

请帮我解决这个问题。 提前致谢…

I am using selenium2.0 with testNG. While on using XPATH or CSS for element to locate its shows error “unable to locate the element” .
I have programmed in Java as
below:

public class mytest {

    public static WebDriver driver;
    public Alert alert;

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        driver=new FirefoxDriver();

    driver.get("http://localhost:4503/xyz.html");

    }

    public static void clickButton(WebDriver driver, String identifyBy, String locator){
        if (identifyBy.equalsIgnoreCase("xpath")){
            driver.findElement(By.xpath(locator)).click();
        }else if (identifyBy.equalsIgnoreCase("id")){
            driver.findElement(By.id(locator)).click();
        }else if (identifyBy.equalsIgnoreCase("name")){
            driver.findElement(By.name(locator)).click();
        }

    }

    public static void typeinEditbox(WebDriver driver, String identifyBy, String locator, String valuetoType){
        if (identifyBy.equalsIgnoreCase("xpath")){
            driver.findElement(By.xpath(locator)).sendKeys(valuetoType);
        }else if (identifyBy.equalsIgnoreCase("id")){
            driver.findElement(By.id(locator)).sendKeys(valuetoType);
        }else if (identifyBy.equalsIgnoreCase("name")){
            driver.findElement(By.name(locator)).sendKeys(valuetoType);
        }

    }


    public static void openApplication(WebDriver driver, String url) {
        driver.get(url);
    }






    @Test
    public void testAcrolinxApplication() throws InterruptedException {
        openApplication(driver,"http://xyz.com");
        typeinEditbox(driver,"name","p_user","xxx");
        typeinEditbox(driver,"name","p_pas","yyy");
        clickButton(driver,"id","input-submit");

/*Up to this its working fine …..

At below line this throws error could not locate the xpath element "//a[@id='cq-gen100']/em/span/span" BUT THIS IS WOKING FINE IN Selenium1.0 api that is with 
selenium.click("//a[@id='cq-gen100']/em/span/span"); */

driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span/span")).click();




    }

}

Please help me on this.
Thanks in advance…

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

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

发布评论

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

评论(2

嗳卜坏 2024-12-11 04:39:01

确保 XPath/CSS 查询正确

下载类似 Firefinder 或其他任何可以直接在页面上测试 XPath 查询的东西。一旦你确定你的查询是正确的,那么你就可以缩小 Selenium 中的问题范围。

缩小 Selenium 中的问题范围

例如,如果您的查询 "//a[@id='cq-gen100']/em/span/span" 不起作用,尝试查询的基础并查看 Selenium 是否找到它(现在不用担心点击)。从那里递增,直到错误再次出现。

例子:

driver.findElement(By.xpath("//a[@id='cq-gen100']"); //有效吗?
driver.findElement(By.xpath("//a[@id='cq-gen100']/em"); //有效吗?
driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span"); //有效吗?
//等等

Make Sure XPath/CSS Query is Correct

Download something like Firefinder or anything else where you can test the XPath query directly on the page. Once you know for sure that your query is correct, then you can narrow down the problem in Selenium.

Narrow Down the Problem in Selenium

For example, if your query "//a[@id='cq-gen100']/em/span/span" is not working, try the base of the query and see if Selenium finds it (don't worry about click for now). Increment from there until the error appears again.

Example:

driver.findElement(By.xpath("//a[@id='cq-gen100']"); //works?
driver.findElement(By.xpath("//a[@id='cq-gen100']/em"); //works?
driver.findElement(By.xpath("//a[@id='cq-gen100']/em/span"); //works?
//etc

ㄖ落Θ余辉 2024-12-11 04:39:01

我用过
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
等待一段时间,Noew 就可以工作了。

I have used
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
to wait for some time, Noew its working.

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