Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/343.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么WebElement对每个循环都使用相同的方法?_Java_Selenium - Fatal编程技术网

Java 为什么WebElement对每个循环都使用相同的方法?

Java 为什么WebElement对每个循环都使用相同的方法?,java,selenium,Java,Selenium,我试图为每个循环使用获取元素,但我总是获取相同的第一个元素 首先,我通过id搜索一个元素 WebElement tableId = driver.findElement(By.id("tablepress-6")); 然后我将通过标记名查找tr元素 List<WebElement> trElements = tableId.findElements(By.tagName("tr")); 因此,我得到: Journal{title='Low Temperature Physics

我试图为每个循环使用
获取元素,但我总是获取相同的第一个元素

首先,我通过
id
搜索一个元素

WebElement tableId = driver.findElement(By.id("tablepress-6"));
然后我将通过
标记名
查找
tr
元素

 List<WebElement> trElements = tableId.findElements(By.tagName("tr"));
因此,我得到:

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>

Sci
主题领域(SJR)
Назва наукового журналу
ISSN(印刷和El)
斯考普斯(SNIP)
WOS_核心
Місто
Шифри 2015
П
物理和天文学
1063-777X

1090-6517

1997-2019年
1997-2019年 Харків /
10; 13 П 物理和天文学
1609-1833
1816-2002

2000-2019年
2008-2019年 Київ 10 П 化学 0040-5760
1573-935X

1967-2019年
2008-2019年 Київ /
10; 16 П 地球和行星科学;物理学和天文学
0884-5913
1934-8401

2009-2019年
2008-2019年 Київ /
10 П 生物化学、遗传学和分子生物学;药
0564-3783
0095-4527

2009-2019年
2007-2019年 Київ /
09; 22 //...
我是不是遗漏了什么?提前谢谢。

试试下面的XPath

List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));
String title = element.findElement(By.xpath("./td[3]")).getText();
试试下面的XPath

List<WebElement> trElements = tableId.findElements(By.xpath("./tbody/tr"));
String title = element.findElement(By.xpath("./td[3]")).getText();

或者,您可以在一个
xpath
语句中执行此操作

List<WebElement> Elements = driver.findElements(By.xpath("//table[@id='tablepress-6']/tbody//tr//td[3]/a"));

 List<Journal> journalList = new ArrayList<>();

    for (WebElement element : Elements) {
    String title = element.getText();

    String city = ...;
    journalList.add(new Journal(title, city));
    }
List Elements=driver.findElements(By.xpath(//table[@id='tablepress-6']]/tbody//tr//td[3]/a));
List journalList=new ArrayList();
for(WebElement:Elements){
String title=element.getText();
字符串城市=。。。;
添加(新期刊(标题,城市));
}

或者,您可以在一条
xpath
语句中执行此操作

List<WebElement> Elements = driver.findElements(By.xpath("//table[@id='tablepress-6']/tbody//tr//td[3]/a"));

 List<Journal> journalList = new ArrayList<>();

    for (WebElement element : Elements) {
    String title = element.getText();

    String city = ...;
    journalList.add(new Journal(title, city));
    }
List Elements=driver.findElements(By.xpath(//table[@id='tablepress-6']]/tbody//tr//td[3]/a));
List journalList=new ArrayList();
for(WebElement:Elements){
String title=element.getText();
字符串城市=。。。;
添加(新期刊(标题,城市));
}

刚刚试过。目前它不起作用:
原因:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id=“tablepress-6”]/tbody/tr/td[3]”
我刚刚一点一点地更改了两个xpath谢谢,Bathiya,现在看起来像是我需要的了。有点奇怪,使用我的代码示例它不起作用。它甚至可以改进如下:
By.xpath(“./td[3]/A”)
刚刚尝试过。目前它不起作用:
原因:org.openqa.selenium.NoSuchElementException:没有这样的元素:无法定位元素:{“方法”:“xpath”,“选择器”:“/*[@id=“tablepress-6”]/tbody/tr/td[3]”
我刚刚一点一点地更改了两个xpath谢谢,Bathiya,现在看起来像是我需要的了。有点奇怪,使用我的代码示例它不起作用。它甚至可以改进如下:
By.xpath(“./td[3]/A”)