英文:
How can I split the string by <br> into a list using Selenium and Java
问题
以下是翻译好的部分:
当我打印出我的字符串时,看起来像这样:
line1
line2
line3
因为它是用Selenium提取的,并且在HTML中看起来像这样:
line1
<br>
line2
<br>
line3
<br>
我想将该字符串转换为包含3个元素的列表,我尝试了以下方法:
List<String> test= new ArrayList<String>(Arrays.asList(mystring.split("<br>")));
但它没有起作用,我得到了一个包含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
<br>
line2
<br>
line3
<br>
I want to transform that string to list of 3 elements, i tried the following:
List<String> test= new ArrayList<String>(Arrays.asList(mystring.split("<br>")));
but it did not work i got a list of 1 element as follows:
[line1
line2
line3]
答案1
得分: 1
Arrays.asList已经返回了列表,无需再次包装。
List
"));
英文:
Arrays.asList
already returns the list, no need to wrap it once again.
List<String> test= Arrays.asList(mystring.split("<br>"));
答案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<String> result = Arrays.asList(myString.split("<br>"));
But the List we get has a limit,this list is Unmodifiable, if you want to do
result.add("element");
you will get an UnsupportedOperationException
if you want to modify your list as follow,you can get it such as
String[] split = myString.split("<br>");
List<String> result = Arrays.stream(split).collect(Collectors.toList());
you can get a modifiable list.
答案3
得分: 0
将文本按<br>
拆分并放入[tag:list]中,需要使用WebDriverWait来等待visibilityOfElementLocated()
,然后可以使用以下任一定位策略:
- 使用_cssSelector_和
split()
:
String[] cssParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("parentElementCssSelector"))).getText().split("<br>");
System.out.println(cssParts);
- 使用_xpath_和
split()
:
String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("parentElementXpath"))).getText().split("<br>");
System.out.println(xpathParts);
参考资料
您可以在以下讨论中找到一些相关信息:
英文:
To split()
the text by <br>
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("parentElementCssSelector"))).getText().split("<br>"); System.out.println(cssParts);
-
Using xpath and
split()
:String[] xpathParts = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("parentElementXpath"))).getText().split("<br>"); System.out.println(xpathParts);
References
You can find a couple of relevant discussions in:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论