首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UTest网站城市域创建动态xpath面临的困难

UTest网站城市域创建动态xpath面临的困难
EN

Stack Exchange QA用户
提问于 2019-03-04 09:56:10
回答 3查看 153关注 0票数 0

我是自动化领域的初学者,我正在尝试自动化其中一个模块,我已经尝试了许多不同的XPATH来为城市字段o UTest站点获取合适的XPATH。我试图自动注册模块作为我的任务之一。我已经尝试了下面的不同xpath示例:

代码语言:javascript
复制
//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]

编写的下列代码:

代码语言:javascript
复制
//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;
    }
}

请参阅链接

EN

回答 3

Stack Exchange QA用户

发布于 2019-03-04 20:36:09

虽然您可能在这里使用Xpath,但看起来最简单的方法可能是使用id策略。我通常不会用这种语言来实现我的自动化,但我认为它只是.

代码语言:javascript
复制
@FindBy(id = 'city')

或者,如果您需要使用xpath,您可以使用等效的..。

代码语言:javascript
复制
//a[@id = 'city']
票数 1
EN

Stack Exchange QA用户

发布于 2019-03-05 10:12:06

因此,如果您需要选择City,则从在输入某个城市名称之后出现的列表中尝试:

代码语言:javascript
复制
@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();
        }
    }
}

替换for循环,如果用提供的方法替换。我试过这段代码,它和你提供的页面一起工作。所以希望它也能对你起作用。

如果需要在已发现城市列表中检查某个城市的确切字符串,请检查el.getText()的返回值。

票数 1
EN

Stack Exchange QA用户

发布于 2019-03-04 22:20:56

而不是您的代码,您应该发布您正在检查的html代码的屏幕截图,无论如何,如果您在获得正确的xpath方面有问题,请使用一个名为“cropath”的铬扩展名,只在尝试自己找到xpath之后才使用它。

票数 0
EN
页面原文内容由Stack Exchange QA提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://sqa.stackexchange.com/questions/38084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档