我有一個方法,我試圖在其中添加12個網路元素:
private List<WebElement> iphoneSnippetList = new ArrayList<>()。
@Test
public void test(){
chromeDriver.get("https://market.yandex.ru/catalog--smartfony/54726/list?hid=91491&glfilter=7893318:153043&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"/span> data-autotest-id="product-snippet"/span>< /article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="產品片段"</article>
<article class="_2vCnw cia-vs cia-cs" data-autotest-id="產品摘要"</article>
我需要將所有12個網路元素添加到我的陣列中,然后確保收到的元素包含 "Iphone "這個名字,但是在添加元素時,出現了例外:
我需要將所有12個網路元素添加到我的陣列中,然后確保收到的元素包含 "Iphone "這個名字。
java.lang.ClassCastException: class org.openqa.selenium.By$ByXPath不能被投到class org。 openqa.selenium.WebElement(org.openqa.selenium.By$ByXPath和org.openqa.selenium.WebElement在加載器module的未命名'app中)。
uj5u.com熱心網友回復:
iphoneSnippetList是Java-Selenium系結的WebElement的一個串列。
我不確定你為什么要使用回圈來添加12個Web元素,相反,使用正確的xpath來查找元素會是一個好的選擇。無論如何,你的代碼中有一個與鑄造有關的問題。
如下, 另外,這個回圈將運行13次而不是12次。如果你想讓它運行12次,請初始化 我認為你也會有xpath的問題,因為你沒有正確使用 嘗試這樣做:
標籤:driver.findElement將回傳web元素,我們將其存盤到變數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)。
i = 1,而不是i = 0xpath 索引。
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)。
