如何将 Web 元素添加到 ArrayList?
How to add a web elements to an ArrayList?
我有一种尝试添加 12 个 Web 元素的方法:
private List<WebElement> iphoneSnippetList = new ArrayList<>();
@Test
public void test(){
chromeDriver.get("https://market.yandex.ru/catalog--smartfony/54726/list?hid=91491&glfilter=7893318%3A153043&onstock=1&local-offers-first=0");
new WebDriverWait(chromeDriver, 15).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[@data-autotest-id='product-snippet'][1]")));
for (int i = 0; i <= 12; i++) {
iphoneSnippetList.add((WebElement) By.xpath("//article[@data-autotest-id='product-snippet'][" + i + "]"));
}
System.out.println(iphoneSnippetList);
}
简化了 DOM 个元素,其中我只需要获取文本:
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
我需要将所有 12 个 Web 元素添加到我的数组中,然后确保接收到的元素包含名称“Iphone”,但是在添加元素时出现异常:
java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement (org.openqa.selenium.By$ByXPath and org.openqa.selenium.WebElement are in unnamed module of loader 'app')
iphoneSnippetList
是 Java-Selenium 绑定中 WebElement
的列表。
我不确定你为什么要使用循环添加 12 个 Web 元素,而不是 findElements
和右 xpath
会是一个不错的选择。不管怎样,你的转换相关代码有问题。
见下文,driver.findElement
将 return web element
,我们将其存储到 variable called Webelement e
中,然后 添加 一样变成iphoneSnippetList
for (int i = 0; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("//article[@data-autotest-id='product-snippet'][" + i + "]"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
此外,此循环将 运行 13 次 而不是 12 次 。如果你想要 运行 12 次,初始化 i = 1
而不是 i = 0
我想你也会遇到 xpath 的问题,因为你没有正确使用 xpath
indexing
。
试试这个:
for (int i = 1; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("(//article[@data-autotest-id='product-snippet'])['" +i+ "']"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
我有一种尝试添加 12 个 Web 元素的方法:
private List<WebElement> iphoneSnippetList = new ArrayList<>();
@Test
public void test(){
chromeDriver.get("https://market.yandex.ru/catalog--smartfony/54726/list?hid=91491&glfilter=7893318%3A153043&onstock=1&local-offers-first=0");
new WebDriverWait(chromeDriver, 15).until(ExpectedConditions.elementToBeClickable(By.xpath("//article[@data-autotest-id='product-snippet'][1]")));
for (int i = 0; i <= 12; i++) {
iphoneSnippetList.add((WebElement) By.xpath("//article[@data-autotest-id='product-snippet'][" + i + "]"));
}
System.out.println(iphoneSnippetList);
}
简化了 DOM 个元素,其中我只需要获取文本:
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="product-snippet"</article>
我需要将所有 12 个 Web 元素添加到我的数组中,然后确保接收到的元素包含名称“Iphone”,但是在添加元素时出现异常:
java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath cannot be cast to class org.openqa.selenium.WebElement (org.openqa.selenium.By$ByXPath and org.openqa.selenium.WebElement are in unnamed module of loader 'app')
iphoneSnippetList
是 Java-Selenium 绑定中 WebElement
的列表。
我不确定你为什么要使用循环添加 12 个 Web 元素,而不是 findElements
和右 xpath
会是一个不错的选择。不管怎样,你的转换相关代码有问题。
见下文,driver.findElement
将 return web element
,我们将其存储到 variable called Webelement e
中,然后 添加 一样变成iphoneSnippetList
for (int i = 0; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("//article[@data-autotest-id='product-snippet'][" + i + "]"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);
此外,此循环将 运行 13 次 而不是 12 次 。如果你想要 运行 12 次,初始化 i = 1
而不是 i = 0
我想你也会遇到 xpath 的问题,因为你没有正确使用 xpath
indexing
。
试试这个:
for (int i = 1; i <= 12; i++) {
WebElement e = driver.findElement(By.xpath("(//article[@data-autotest-id='product-snippet'])['" +i+ "']"));
iphoneSnippetList.add(e);
}
System.out.println(iphoneSnippetList);