0

I am beginner in automation field, I am trying to automated one of the module, I have tried many different XPATH to fetch proper XPATH for City field o UTest site. Im trying to automate registration module as one of my assignment. I have tried my different xpath example below:

//xpath="//body/div[@class='pac-container pac-logo']/div/span[2]/span"
//div[@class='pac-item']/span[3]
//div[@class='pac-item']/span
//div[@class='pac-container pac-logo']/div/span[2]/../span[3]

the following code written:

//xpath to send data in field
@FindBy(xpath="//input[@placeholder='Enter a location']")
WebElement city;

//xpath for list to appear
@FindBy(xpath="div[@class='pac-item']/span")
List<WebElement> City;

public void EnterSignupTwo(String CiTy) throws Exception {
    city.clear();
    city.sendKeys(CiTy);
    Thread.sleep(3000);
    System.out.println("the size of List " +City.size());//printing list size 
    for(int i=0; i<City.size(); i++ ) {
        String cities = City.get(i).getText(); //storing list element in String
        System.out.println("Converted value of city :"+cities); //print of total list

    if(cities.contains(CiTy)) //if parameterized match with Available then click
    {
        System.out.println("enter specific value of city :"+City.get(i).getText());
        City.get(i).click();
        break;
    }
}

kindly refer the link

1

While you could potentially use Xpath here it looks like the simplest way may be using the id strategy instead. I don't typically use this language for my automation but I think it'd just be...

@FindBy(id = 'city')

alternatively if you need to use xpath you could use the equivalent...

//a[@id = 'city']
  • Thank you for the reply.The thing is,the xpath which you have shown is for field. I have done that.I am struggling to get xpath for list Web Element. – shaikhsaliha Mar 5 at 4:06
1

So, if you need to select City, from list which appears after some City name is entered try:

@FindBy(id="city")
private WebElement city;

@FindBy(xpath="//div[@class='pac-item']")
private List<WebElement> foundCities;

public void enterCityName(String cityName) {
    city.sendKeys(cityName);
}

/**
* This method select city from list of possible options
* @param cityName 
*/
public void selectCity(String cityName) {
    for(WebElement el: foundCities) {
        // Use equals or contains, it depends on what you want to search. If I enter "Prague" into field, then multiple cities are found.
        // So there is need for more concrete string, like PragueCzechia
        if(el.getText().contains(cityName)) {
            //this will select appropriate city
            el.click();
        }
    }
}

Replace your for loop, and if with provided method. I tried this code and it worked with page you provided. So hopefully it will work for you too.

If you need to check exact string for some city in list of found cities, check returned value for el.getText().

  • This is not working for my module,after changes also the issue is same.The Case is my parameterized value is coming from excel using DataProvider. In Excel i have stored City as:Colombo Sri Lanka – shaikhsaliha Mar 5 at 12:58
  • So you can find element, but they are not what you expect? First found result for Colombo after you use el.getText() is "ColomboSri Lanka". Then change your data in excel, or remove whitespaces for both excel string and string returned from getText(). This can help to do that .replaceAll("\\s+",""); – tugo Mar 5 at 13:11
  • The Problem encountered is in xpath.The above replaceAll couldnt solve it.please see the link which i provided to automate Utest site.In uTest it's Registration module page2 consist of city,zip and country.Please do provide with some other solution – shaikhsaliha Mar 6 at 11:04
  • I am sorry, but I am lost now. I thought that question is about selecting of City from list on page you provided, code above should work for locating city field and then locating of list with cities. But you need correct string, try if it work with city I used - as city use Prague, and for selectCity() use PragueCzechia. Do you receive any error? Let other fields out, until you make selecting of City work. – tugo Mar 6 at 11:56
0

Rather than your code, you should post a screenshot of the html code you're inspecting, anyways, if you're having issues getting correct xpath, use a chrome extension called 'cropath' use it only after trying to find the xpath by yourself

  • I have used Chropath that is not able to solve my problem.I'll put html code so that it will be more clear – shaikhsaliha Mar 5 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.