简体   繁体   English

使用列表设置类似的weblement <webelement> 硒中

[英]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.

相关问题 Java | 硒| 使用Stream从Weblement列表中返回单个Webelement - Java | Selenium | Using Stream to return single Webelement from a list of weblements webelement 列表中的一些 wblements 值变为空白如何等待 webelement 的每个值出现在 selenium 中的 webelements 中 - some of the value of weblements in list of webelement are coming blank How to wait for each value of webelement to appear in webelements in selenium Selenium中的“ StaleElementReferenceException”用于列表<WebElement> - “StaleElementReferenceException” in Selenium for a List<WebElement> Selenium webdriver 将 webelement 存储在 webelement 列表中 - Selenium webdriver store webelement in webelement list 使用 webElement(Selenium) 获取所有 &#39;x&#39;-tags 内容作为列表 - Using webElement(Selenium) get all 'x'-tags content as a list 如何放置清单 <webelement> 使用硒网络驱动程序/ Java进入ArrayList? - How to put List<webelement> into ArrayList using selenium web driver/java? 无法单击硒列表Webelement中的第二个Webelement(anchor标签) - Not able to click on the 2nd webelement(anchor tag) in a list webelement in selenium 选择列表WebElement的子级-Java-Selenium WebDriver - Select child of List WebElement - Java - Selenium WebDriver 如何更改硒列表中的值<Webelement> - how to change value in selenium List <Webelement> Java Selenium Webdriver手动填充列表WebElement - Java Selenium webdriver filling list webelement manually
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM