[英]Setting up similar weblements using a list<webelement> in selenium
I've several page elements / records having xpath / id's like this 我有几个页面元素/具有xpath / id的记录是这样的
//*[@id='xyz-model-car-card-0']/car
//*[@id='xyz-model-car-card-1']/car
//*[@id='xyz-model-car-card-2']/car
and i am looking to add these webelements using List so that i can take any action on them. 我希望使用列表添加这些网络元素,以便我可以对它们执行任何操作。
List<WebElement> item = Arrays.asList();
// incase i want to navigate to 5 cards..
for(int i=0;i<5;i++) {
item.add(By.id('xyz-model-car-card-'+i));
}
but, i am unable to add it.. giving below error.. 但是,我无法添加它。
*add (org.openqa.selenium.WebElement)
in List cannot be applied to (org.openqa.selenium.By)*
Whats the best way to deal with such webelements name if we want to load them in one go. 如果要一次性加载这些Webelements名称,最好的方法是什么。
enter code here
Pl. PL。 share some example.. Thanks in advance!
分享一些例子..预先感谢!
Assuming a webdriver named 'driver': 假设一个名为“ driver”的网络驱动程序:
List<WebElement> xyzModels = driver.findElements(By.xpath("//*[starts-with(@id='xyz-model-car-card-')]/car));
For (WebElement car : xyzModels) {
//your code here...
}
You are trying to add the By object to your WebElement list: 您试图将By对象添加到WebElement列表中:
item.add(By.id('xyz-model-car-card-'+i));
You need to add the web element instead (assuming you are using driver when you instantiate your new browser object): 您需要添加Web元素(假设在实例化新的浏览器对象时正在使用驱动程序):
item.add(driver.findElement(By.id('xyz-model-car-card-'+i)));
But, I agree with Bill Hileman's answer. 但是,我同意比尔·希勒曼的回答。 Best way to add the elements to a list is to do this:
将元素添加到列表的最佳方法是:
List<WebElement> myList = driver.findElements(By.....)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.