Java-从列表中读取第一个值,并使用Intellij将其存储在一个变量中。

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

Java- Read the first value from the list and store in a variable using Intellij

问题

我在尝试检索和保存网页元素列表中的第一个值时遇到了问题。我编写了一个XPath来检索,但它却获取了空值。令人惊讶的是,同样的XPath在Chrome浏览器中却可以正常工作。所以请指导我理解出了什么问题以及问题出在哪里。以下是HTML代码和XPath。

在下面的值列表中,我应该选择第一个值并将其存储在一个变量中。(即 Coffee)

<ul id="ui-id-1" tabindex="0" class="ui-menu ui-widget ui-widget-content ui-autocomplete ui-front" style="width: 132.667px; top: 440.885px; left: 531px; display: none;">
   <li class="ui-menu-item"><a id="ui-id-6" tabindex="-1" class="ui-menu-item-wrapper"></a></li>
   <li class="ui-menu-item"><a id="ui-id-7" tabindex="-1" class="ui-menu-item-wrapper">Coffee</a></li>
   <li class="ui-menu-item"><a id="ui-id-8" tabindex="-1" class="ui-menu-item-wrapper">Tea</a></li>
   <li class="ui-menu-item"><a id="ui-id-9" tabindex="-1" class="ui-menu-item-wrapper">Milk</a></li>
</ul>

XPath:

(//ul[@id='ui-id-1']//li//a[@class='ui-menu-item-wrapper'])[2]

我对为什么这个XPath在Chrome浏览器中可以正确地突出显示感到困惑,但是在脚本运行时却返回空值。请帮助我解决这个问题。

注意:在读取值之前,我首先点击了选择下拉菜单,然后尝试读取值。

英文:

I'm facing an issue while trying to retrieve and save the first value from the web element list. I wrote a XPATH to retrieve but it is fetching the null value. Surprisingly the same xpath is working correctly from the chrome browser. So please guide me to understand where and what went wrong.
Please find the below HTML code and the XPath.

From the below list of values, I should select the first value and store in a variable. (i.e. Coffee)

&lt;ul id=&quot;ui-id-1&quot; tabindex=&quot;0&quot; class=&quot;ui-menu ui-widget ui-widget-content ui-autocomplete ui-front&quot; style=&quot;width: 132.667px; top: 440.885px; left: 531px; display: none;&quot;&gt;
   &lt;li class=&quot;ui-menu-item&quot;&gt;&lt;a id=&quot;ui-id-6&quot; tabindex=&quot;-1&quot; class=&quot;ui-menu-item-wrapper&quot;&gt;&lt;/a&gt;&lt;/li&gt;
   &lt;li class=&quot;ui-menu-item&quot;&gt;&lt;a id=&quot;ui-id-7&quot; tabindex=&quot;-1&quot; class=&quot;ui-menu-item-wrapper&quot;&gt;Coffee&lt;/a&gt;&lt;/li&gt;
   &lt;li class=&quot;ui-menu-item&quot;&gt;&lt;a id=&quot;ui-id-8&quot; tabindex=&quot;-1&quot; class=&quot;ui-menu-item-wrapper&quot;&gt;Tea&lt;/a&gt;&lt;/li&gt;
   &lt;li class=&quot;ui-menu-item&quot;&gt;&lt;a id=&quot;ui-id-9&quot; tabindex=&quot;-1&quot; class=&quot;ui-menu-item-wrapper&quot;&gt;Milk&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

XPATH  (//ul[@id=&#39;ui-id-1&#39;]//li//a[@class=&#39;ui-menu-item-wrapper&#39;])[2]

>I am so confused on, why this xpath is highlighting correctly in chrome browser but the same is giving null value while running from the script. Please help me to resolve the issue.

Note:- Before reading the value, first I'm clicking the select dropdown and then trying to read the value.

答案1

得分: 0

您可以输入以下代码:
        WebElement ul_element = driver.findElement(By.xpath("x_path_to_your_ul_tab"));
        List<WebElement> li_list = ul_element.findElements(By.tagName("li"));
        WebElement first_li_element = li_list.get(0);
您还可以按以下方式迭代所有li元素:
        for (WebElement li : li_list ) {
            if (li.getText().equals(role)) {
                li.click();
            }
        }

<details>
<summary>英文:</summary>

You can ty below code
        WebElement ul_element = driver.findElement(By.xpath(&quot;x_path_to_your_ul_tab&quot;));
        List&lt;WebElement&gt; li_list = ul_element.findElements(By.tagName(&quot;li&quot;));
        WebElement first_li_element = li_list.get(0);
also you can iterate all li elements like below 
        for (WebElement li : li_list ) {
            if (li.getText().equals(role)) {
                li.click();
            }
        }

</details>



huangapple
  • 本文由 发表于 2020年10月20日 13:56:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/64439259.html
匿名

发表评论

匿名网友

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

确定