我想提取所有在<ul>下的<li>元素文本。

huangapple go评论147阅读模式
英文:

I want to extract all <li> element text that are under <ul>

问题

<div class="headerarea" style="" xpath="1">
    <h2>
        <span id="ctl00_ctl00_phDesktop_lblModuleTitle">Abstract Setup</span>
    </h2>

    <ul>

        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl01_btnModuleNavigation" class="headerarea_active" href="https://staging.m-anage.com/Modules/Abstract/Setup/basics.aspx">Basic</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl02_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Tracks">Tracks</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl03_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/steps.aspx">WIZARD</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl04_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/keywords.aspx">KEYWORDS</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl05_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/categories.aspx">CATEGORIES</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl06_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/conditions.aspx">CONDITIONS</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl07_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/interests.aspx">Interests</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl08_btnModuleNavigation" href="https://staging.m-anage.com/Modules/Abstract/Setup/templates.aspx">Templates</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl09_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Index">Submission fee</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl10_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Mail/MailServerSetup/Index?pModuleType=Abstract" style="">SMTP Setup</a></li>


        <li>
            <a id="ctl00_ctl00_phDesktop_rModuleNavigation_ctl11_btnModuleNavigation" href="https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Coauthor">Co-author</a></li>


    </ul>

</div>
List<WebElement> tags = driver.findElements(By.xpath("//div[@class='headerarea']/ul/li")); 
for(int i=0; i<tags.size(); i++) { 
    // Click on the current element
    tags.get(i).click(); 
}

Link to image

英文:

I need to click on all elements BASIC, TRACKS, ...
My idea is to extract all elements in list then using list count and loop, I'll click on each and every element.
Need to check that each and every element is working even if new element is added I don't want to check code.

<!-- language: lang-html -->

&lt;div class=&quot;headerarea&quot; style=&quot;&quot; xpath=&quot;1&quot;&gt;
&lt;h2&gt;
&lt;span id=&quot;ctl00_ctl00_phDesktop_lblModuleTitle&quot;&gt;Abstract Setup&lt;/span&gt;
&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl01_btnModuleNavigation&quot; class=&quot;headerarea_active&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/basics.aspx&quot;&gt;Basic&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl02_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Tracks&quot;&gt;Tracks&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl03_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/steps.aspx&quot;&gt;WIZARD&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl04_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/keywords.aspx&quot;&gt;KEYWORDS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl05_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/categories.aspx&quot;&gt;CATEGORIES&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl06_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/conditions.aspx&quot;&gt;CONDITIONS&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl07_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/interests.aspx&quot;&gt;Interests&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl08_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/Modules/Abstract/Setup/templates.aspx&quot;&gt;Templates&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl09_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Index&quot;&gt;Submission fee&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl10_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/testselenium/en-US/Mail/MailServerSetup/Index?pModuleType=Abstract&quot; style=&quot;&quot;&gt;SMTP Setup&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;
&lt;a id=&quot;ctl00_ctl00_phDesktop_rModuleNavigation_ctl11_btnModuleNavigation&quot; href=&quot;https://staging.m-anage.com/testselenium/en-US/Abstract/AbstractSetup/Coauthor&quot;&gt;Co-author&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;

<!-- end snippet -->

I tried travelling to child path but no success

Here is the java code that I tried.

    List&lt;WebElement&gt; tags = 
driver.findElements(By.xpath(&quot;//div[@class=&#39;headerarea&#39;]/ul/li&quot;)); 
for(int i=0;i&lt;tags.size();i++) { 
while(???) { 
//driver.findElement(By.xpath(&quot;//div[@class=&#39;headerarea&#39;]/ul/li&quot;)).click(); 
}
}

答案1

得分: 0

List<WebElement> tags = driver.findElements(By.cssSelector(".headerarea ul>li"));
for(WebElement e : tags) {
e.click();
}

英文:
List&lt;WebElement&gt; tags = driver.findElements(By.cssSelector(&quot;.headerarea ul&gt;li&quot;)); 
for(WebElement e : tags) {
e.click();
}

答案2

得分: 0

试试以下代码:

List<WebElement> links = driver.findElements(By.tagName("li"));
for (int i = 1; i < links.size(); i++) {
    System.out.println(links.get(i).getText());
}

如果你遇到同步问题你也可以使用 `WebDriverWait`。

```java
WebDriverWait wait = new WebDriverWait(driver, 10);
List<WebElement> links = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName("li")));
for (int i = 1; i < links.size(); i++) {
    System.out.println(links.get(i).getText());
}
英文:

Try below code :

List&lt;WebElement&gt; links = driver.findElements(By.tagName(&quot;li&quot;));
for (int i = 1; i &lt; links.size(); i++)
{
System.out.println(links.get(i).getText());
}

You can also use WebDriverWait if you are facing synchronization issue.

 WebDriverWait wait = new WebDriverWait(driver, 10);
List&lt;WebElement&gt; links = wait.until(ExpectedConditions.presenceOfAllElementsLocatedBy(By.tagName(li)));
for (int i = 1; i &lt; links.size(); i++)
{
System.out.println(links.get(i).getText());
}

huangapple
  • 本文由 发表于 2020年7月27日 21:13:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63116096.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定