简体   繁体   中英

any way to catch a WebElement without using XPath or CssPath

I am very new at Java and Selenium so my apologies in advance if my question sounds a bit primary.

I am using Selenium and Java to write a test. The issue is the application I'm testing is not finished yet and its developers change the code daily. When I use XPath or CssPath it may change after a while, so I have to go through all my test program and change those XPathes or Css ones.

Is there any other way that I can have access to a WebElement without using Css or XPath?

If you do not have to use Java, you may consider using Robot Framework instead, it offers you the possibility to write code in high level language, which is faster.

With Robot Framework, you address an element using Xpath, ID, name, css selector, tag, dom, jquery and etc.

I would think developers would not change tags so often, would they?

在此处输入图片说明

There are several different ways of selecting a webelement in Selenium:

  • Class Name (ie. By.className("content") => <div class="content"> )
  • CSS selector (ie. By.cssSelector(".content") => <div class="content"> )
  • ID (ie. By.id("okButton") => <button id="okButton" /> )
  • Link Text (ie. By.linkText("Cancel") => <a href="#">Cancel</a> )
  • Partial Link Text (ie. By.partialLinkText("Ca") => <a href="#">Cancel</a> )
  • Tag Name (ie. By.tagName("img") => <img src="logo.png" /> )
  • XPath (ie. By.xpath("//img[@src='logo.png']") => <img src="logo.png" /> )

In addition, if you can get away with it, there's also checking of the page title, viewing the HTML source (via driver.getPageSource() ), and navigation by simulating mouse positions and events.

Yes, you can ID's, name(text). If you don't have ID's you can ask your developers to add for any element in your application

You should explore the Page Object Model. There are a lot of sources on the web but a good starting place is the page on the Selenium wiki .

Basically each page is defined as its own class and in that class each element that you want access to is defined. There are many advantages to using this model but in your case a really big advantage is that when things change on a regular basis, all you have to do is find the elements' definition in the page object and change it. Think of it like an API for the page. You change the references behind the scenes (in the page object) but the code built on that API (the scripts/tests) doesn't change. It's a very quick way to update lots of scripts based on a dynamic site. For a quick example using the google home page.

The google home page page object would look something like this

public class GoogleHomePage
{
    private By searchBox = By.id("lst-ib");
    private By searchButton = By.cssSelector("input[name='btnK']");
    private WebDriver driver;

    public GoogleHomePage(WebDriver driver)
    {
        this.driver = driver;
    }

    public void doSearch(String query)
    {
        getSearchBox().sendKeys(query);
        getSearchButton().click();
    }

    public WebElement getSearchBox()
    {
        return driver.findElement(searchBox);
    }

    public WebElement getSearchButton()
    {
        return driver.findElement(searchButton);
    }
}

The test itself would look something like this

public class SimpleTest
{
    public static void main(String[] args)
    {
        FirefoxDriver driver = new FirefoxDriver();
        GoogleHomePage homePage = new GoogleHomePage(driver);
        driver.get("http://www.google.com");
        homePage.doSearch("stack overflow");
        System.out.println(driver.getTitle());
    }
}

So tomorrow when google redesigns their search home page and the IDs change, you go to the two lines in the GoogleHomePage class and change locators for searchBox and searchButton and you are done. Your existing tests continue to work with that one change.


EDIT 1: If you don't want to go full page objects, define your locators at the top of the page so you only have one place to change them (see the top of the GoogleHomePage class for some examples of locators/By). When a site is that dynamic, there's really not much you can do about it other than ask dev to put something on particular elements that will never change... IDs, names, etc. but others have already suggested that.

If all of the elements are changing frequently, I suggest you stopping your automation project. A vvvvvery changeable product is not suitable for automated test.

So if it's your duty to perform auto test or regression and you must control it by java&selenium-webdriver, I suggest you composing a simple framework-Even a single webpage and sevral data files. Wrap the internal logic and your operations and maintain your Xpath/CSS or other selectors in the WebUI. Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM