我是自动化领域的初学者,我正在尝试自动化其中一个模块,我已经尝试了许多不同的XPATH来为城市字段o UTest站点获取合适的XPATH。我试图自动注册模块作为我的任务之一。我已经尝试了下面的不同xpath示例:
//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]编写的下列代码:
//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;
}
}请参阅链接
发布于 2019-03-04 20:36:09
虽然您可能在这里使用Xpath,但看起来最简单的方法可能是使用id策略。我通常不会用这种语言来实现我的自动化,但我认为它只是.
@FindBy(id = 'city')或者,如果您需要使用xpath,您可以使用等效的..。
//a[@id = 'city']发布于 2019-03-05 10:12:06
因此,如果您需要选择City,则从在输入某个城市名称之后出现的列表中尝试:
@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()的返回值。
发布于 2019-03-04 22:20:56
而不是您的代码,您应该发布您正在检查的html代码的屏幕截图,无论如何,如果您在获得正确的xpath方面有问题,请使用一个名为“cropath”的铬扩展名,只在尝试自己找到xpath之后才使用它。
https://sqa.stackexchange.com/questions/38084
复制相似问题