gpt4 book ai didi

java - 为什么 WebElement 对于每个循环使用相同的 Selenium?

转载 作者:行者123 更新时间:2023-12-02 01:04:08 24 4
gpt4 key购买 nike

我尝试使用 for every 循环获取元素,但我总是得到相同的第一个元素。

首先,我通过 id 搜索元素。

WebElement tableId = driver.findElement(By.id("tablepress-6"));

然后我将通过 tagName 查找 tr 元素。

 List<WebElement> trElements = tableId.findElements(By.tagName("tr"));

最后使用 for every 循环,我尝试获取具有必填字段的 td 元素:

List<Journal> journalList = new ArrayList<>();
//...
for (WebElement element : trElements) {
String title = element.findElement(By.xpath("//*[@id=\"tablepress-6\"]/tbody/tr/td[3]")).getText();
//String title = element.findElement(By.xpath("//table[@id='tablepress-6']/tbody/tr/td/a")).getText();
String city = ...;
journalList.add(new Journal(title, city));
}

结果我得到:

Journal{title='Low Temperature Physics', city='...'}
Journal{title='Low Temperature Physics', city='...'}
Journal{title='Low Temperature Physics', city='...'}
Journal{title='Low Temperature Physics', city='...'}

而不是预期的:

Journal{title='Low Temperature Physics', city='...'}
Journal{title='Ukrainian Journal of Physical Optics', city='...'}
Journal{title='Theoretical and Experimental Chemistry', city='...'}
Journal{title='Kinematics and Physics of Celestial Bodies', city='...'}

这是我的 HTML 代码:

<table id=tablepress-6 class="tablepress tablepress-id-6">
<thead>
<tr class="row-1 odd">
<th class=column-1>Sci</th>
<th class=column-2>Subject area (SJR)</th>
<th class=column-3>Назва наукового журналу</th>
<th class=column-4>ISSN (Print&amp;El)</th>
<th class=column-5>Scopus(SNIP)</th>
<th class=column-6>WOS_Core</th>
<th class=column-7>Місто</th>
<th class=column-8>Шифри 2015</th>
</tr>
</thead>
<tbody class=row-hover>
<tr class="row-2 even">
<td class=column-1>П</td>
<td class=column-2>Physics and astronomy</td>
<td class=column-3><a href="http://ltp.aip.org/" target=_blank rel="nofollow noopener noreferrer">Low Temperature Physics</a></td>
<td class=column-4>1063-777X
<br/> 1090-6517
<br/>
</td>
<td class=column-5><a href="https://www.scopus.com/sourceid/13789" target=_blank rel="nofollow noopener noreferrer">0,617</a>
<br/> 1997-2019
</td>
<td class=column-6><a href="http://ip-science.thomsonreuters.com/cgi-bin/jrnlst/jlresults.cgi?PC=MASTER&amp;ISSN=1063-777X" target=_blank rel="nofollow noopener noreferrer">SCIE (0,860)</a>
<br/> 1997-2019
</td>
<td class=column-7>Харків /
<br/> Нью-Йорк
</td>
<td class=column-8>10; 13</td>
</tr>
<tr class="row-3 odd">
<td class=column-1>П</td>
<td class=column-2>Physics and astronomy</td>
<td class=column-3><a href="http://www.ifo.lviv.ua/journal/" target=_blank rel="nofollow noopener noreferrer">Ukrainian Journal of Physical Optics</a>
<br/> Український журнал фізичної оптики</td>
<td class=column-4>1609-1833
<br/> 1816-2002
<br/>
</td>
<td class=column-5><a href="https://www.scopus.com/sourceid/11400153315" target=_blank rel="nofollow noopener noreferrer">0,593</a>
<br/> 2000-2019
</td>
<td class=column-6><a href="http://ip-science.thomsonreuters.com/cgi-bin/jrnlst/jlresults.cgi?PC=MASTER&amp;ISSN=1609-1833" target=_blank rel="nofollow noopener noreferrer">SCIE (0,488)</a>
<br/> 2008-2019
</td>
<td class=column-7>Київ</td>
<td class=column-8>10</td>
</tr>
<tr class="row-4 even">
<td class=column-1>П</td>
<td class=column-2>Chemistry</td>
<td class=column-3><a href="http://link.springer.com/journal/11237" target=_blank rel="nofollow noopener noreferrer">Theoretical and Experimental Chemistry</a></td>
<td class=column-4>0040-5760
<br/> 1573-935X
<br/>
</td>
<td class=column-5><a href="https://www.scopus.com/sourceid/21969" target=_blank rel="nofollow noopener noreferrer">0,198</a>
<br/> 1967-2019
</td>
<td class=column-6><a href="http://ip-science.thomsonreuters.com/cgi-bin/jrnlst/jlresults.cgi?PC=MASTER&amp;ISSN=0040-5760" target=_blank rel="nofollow noopener noreferrer">SCIE (0,878)</a>
<br/> 2008-2019
</td>
<td class=column-7>Київ /
<br/> Нью-Йорк
</td>
<td class=column-8>10; 16</td>
</tr>
<tr class="row-5 odd">
<td class=column-1>П</td>
<td class=column-2>Earth and planetary sciences; Physics and Astronomy</td>
<td class=column-3><a href="http://www.allertonpress.com/journals/kin.htm" target=_blank rel="nofollow noopener noreferrer">Kinematics and Physics of Celestial Bodies</a>
<br/> Кінематика і фізика небесних тіл</td>
<td class=column-4>0884-5913
<br/> 1934-8401
<br/>
</td>
<td class=column-5><a href="https://www.scopus.com/sourceid/19700173017" target=_blank rel="nofollow noopener noreferrer">0,432</a>
<br/> 2009-2019
</td>
<td class=column-6><a href="http://ip-science.thomsonreuters.com/cgi-bin/jrnlst/jlresults.cgi?PC=MASTER&amp;ISSN=0884-5913" target=_blank rel="nofollow noopener noreferrer">SCIE (0,654)</a>
<br/> 2008-2019
</td>
<td class=column-7>Київ /
<br/> Нью-Йорк
</td>
<td class=column-8>10</td>
</tr>
<tr class="row-6 even">
<td class=column-1>П</td>
<td class=column-2>Biochemistry, genetics and molecular biology; Medicine</td>
<td class=column-3><a href="https://link.springer.com/journal/11956" target=_blank rel="nofollow noopener noreferrer">Cytology and Genetics</a>
<br/> Цитологія і генетика</td>
<td class=column-4>0564-3783
<br/> 0095-4527
<br/>
</td>
<td class=column-5><a href="https://www.scopus.com/sourceid/19061" target=_blank rel="nofollow noopener noreferrer">0,229</a>
<br/> 2009-2019
</td>
<td class=column-6><a href="http://ip-science.thomsonreuters.com/cgi-bin/jrnlst/jlresults.cgi?PC=MASTER&amp;ISSN=0095-4527" target=_blank rel="nofollow noopener noreferrer">SCIE (0,239)</a>
<br/> 2007-2019
</td>
<td class=column-7>Київ /
<br/> Нью-Йорк
</td>
<td class=column-8>09; 22</td>
</tr>
//...
</tbody>
</table>

我在这里遗漏了什么吗?提前致谢。

最佳答案

尝试下面的 XPath

........................

List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));

......................

String title = element.findElement(By.xpath("./td[3]")).getText();

关于java - 为什么 WebElement 对于每个循环使用相同的 Selenium?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60266766/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com