我有几个xpath / id这样的页面元素/记录
//*[@id='xyz-model-car-card-0']/car
//*[@id='xyz-model-car-card-1']/car
//*[@id='xyz-model-car-card-2']/car
我希望使用List添加这些webelements,以便我可以对它们采取任何行动。
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));
}
但是,我无法添加它..给出以下错误..
*add (org.openqa.selenium.WebElement)
in List cannot be applied to (org.openqa.selenium.By)*
如果我们想要一次性加载它们,那么处理这些webelements名称的最佳方法是什么。enter code here
PL。分享一些例子..提前致谢!
答案 0 :(得分:3)
假设一个名为&#39; driver&#39;:
的webdriverdef _hack_windows_subprocess():
"""HACK: python 2.7 file descriptors.
This magic hack fixes https://bugs.python.org/issue19575
by adding HANDLE_FLAG_INHERIT to all already opened file descriptors.
"""
# Extracted from https://github.com/secdev/scapy/issues/1136
import stat
from ctypes import windll, wintypes
from msvcrt import get_osfhandle
HANDLE_FLAG_INHERIT = 0x00000001
for fd in range(100):
try:
s = os.fstat(fd)
except:
break
if stat.S_ISREG(s.st_mode):
handle = wintypes.HANDLE(get_osfhandle(fd))
mask = wintypes.DWORD(HANDLE_FLAG_INHERIT)
flags = wintypes.DWORD(0)
windll.kernel32.SetHandleInformation(handle, mask, flags)
答案 1 :(得分:1)
您正在尝试将By对象添加到WebElement列表中:
item.add(By.id('xyz-model-car-card-'+i));
您需要添加Web元素(假设您在实例化新浏览器对象时使用驱动程序):
item.add(driver.findElement(By.id('xyz-model-car-card-'+i)));
但是,我同意Bill Hileman的回答。将元素添加到列表的最佳方法是:
List<WebElement> myList = driver.findElements(By.....)