如何使用Selenium和Java将字符串按<br>分割为列表。

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

How can I split the string by <br> into a list using Selenium and Java

问题

以下是翻译好的部分:

当我打印出我的字符串时,看起来像这样:

line1
line2
line3

因为它是用Selenium提取的,并且在HTML中看起来像这样:

line1
&lt;br&gt;
line2
&lt;br&gt;
line3
&lt;br&gt;

我想将该字符串转换为包含3个元素的列表,我尝试了以下方法:

List&lt;String&gt; test= new ArrayList&lt;String&gt;(Arrays.asList(mystring.split(&quot;&lt;br&gt;&quot;)));

但它没有起作用,我得到了一个包含1个元素的列表,如下所示:

[line1
line2
line3]
英文:

My string when printed looks like:

line1
line2
line3

as it's a extracted with selenium and it looks in html:

line1
&lt;br&gt;
line2
&lt;br&gt;
line3
&lt;br&gt;

I want to transform that string to list of 3 elements, i tried the following:

List&lt;String&gt; test= new ArrayList&lt;String&gt;(Arrays.asList(mystring.split(&quot;&lt;br&gt;&quot;)));

but it did not work i got a list of 1 element as follows:

[line1
line2
line3]

答案1

得分: 1

Arrays.asList已经返回了列表,无需再次包装。

List test = Arrays.asList(mystring.split("
"));

英文:
Arrays.asList

already returns the list, no need to wrap it once again.

List&lt;String&gt; test= Arrays.asList(mystring.split(&quot;&lt;br&gt;&quot;));

答案2

得分: 0

你可以直接使用Arrays.asList,如下所示:

List<String> result = Arrays.asList(myString.split("<br>"));

但是我们得到的List有限制,这个列表是不可修改的,如果你想这样做:

result.add("element");

你会得到一个UnsupportedOperationException异常。

如果你想要修改你的列表,可以按照以下方式获取:

String[] split = myString.split("<br>");
List<String> result = Arrays.stream(split).collect(Collectors.toList());

你可以得到一个可修改的列表。

英文:

you can use Arrays.asList directly as follow

List&lt;String&gt; result = Arrays.asList(myString.split(&quot;&lt;br&gt;&quot;));

But the List we get has a limit,this list is Unmodifiable, if you want to do

result.add(&quot;element&quot;);

you will get an UnsupportedOperationException

if you want to modify your list as follow,you can get it such as

String[] split = myString.split(&quot;&lt;br&gt;&quot;);
List&lt;String&gt; result = Arrays.stream(split).collect(Collectors.toList());

you can get a modifiable list.

答案3

得分: 0

将文本按&lt;br&gt;拆分并放入[tag:list]中,需要使用WebDriverWait来等待visibilityOfElementLocated(),然后可以使用以下任一定位策略

  • 使用_cssSelector_和split()
String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("parentElementCssSelector"))).getText().split("&lt;br&gt;");
System.out.println(cssParts);
  • 使用_xpath_和split()
String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("parentElementXpath"))).getText().split("&lt;br&gt;");
System.out.println(xpathParts);

参考资料

您可以在以下讨论中找到一些相关信息:

英文:

To split() the text by &lt;br&gt; and place into a [tag:list] you need to induce WebDriverWait for the visibilityOfElementLocated() and you can use either of the following Locator Strategies:

  • Using cssSelector and split():

    String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(&quot;parentElementCssSelector&quot;))).getText().split(&quot;&lt;br&gt;&quot;);
    System.out.println(cssParts);
    
  • Using xpath and split():

    String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(&quot;parentElementXpath&quot;))).getText().split(&quot;&lt;br&gt;&quot;);
    System.out.println(xpathParts);
    

References

You can find a couple of relevant discussions in:

huangapple
  • 本文由 发表于 2020年7月26日 17:37:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63098451.html
匿名

发表评论

匿名网友

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

确定