简体   繁体   English

WebDriver选择并复制div中的文本

[英]WebDriver select and copy text from div

I need to select a specific text from a DIV, here is the DIV source: 我需要从DIV中选择特定的文本,这是DIV的来源:

<html>
<div class="roamingHostIdContainer ng-binding">
                                        Host ID: 3K9X-Q8LD-6AX6-3UGP-UL5B-YE3Z-UWCD-DGDU-AB8Y-FJD2-7W97-A63J-RVZA
                                    </div>
</html>

as you can see, the div has a bit too many spaces, back to the point, the thing is I need to select the ID value and the copy it. 如您所见,div的空间太多了,回到这一点,我需要选择ID值并将其复制。

My starting point was this question: How to manipulate user selected text using webdriver? 我的出发点是这个问题: 如何使用webdriver操作用户选择的文本? then I moved to this: How to move cursor in Selenium Webdriver 然后我转到了: 如何在Selenium Webdriver中移动光标

I believe I can do it with a javascript executor but i'm a bit lost on how to use it, my idea is to create an element just with the "Host ID: " text and another one just with the RVZA text, but then I realize I can't create an element based only in that text, (can I?) because both elements will be the same element 我相信我可以使用javascript执行器来完成此操作,但是我对如何使用它有点迷失了,我的想法是仅使用“主机ID:”文本创建一个元素,而仅使用RVZA文本创建另一个元素,但是我意识到我不能仅基于该文本创建一个元素(可以吗?),因为两个元素都是同一个元素

So if any of you could guide me on the right path i'll appreciate it 因此,如果你们中的任何人可以引导我走上正确的道路,我将不胜感激

This has nothing to do with Selenium, this is purely a Java String manipulation problem. 这与Selenium无关,这纯粹是Java String操作问题。

String myText = driver.findElement(By.className("roamingHostIdContainer")).getText();
String myIdx = myText.indexOf("ID:");
String myId = myText.substring(myIdx + 4).trim();

driver.findElement(some-other-area).sednKeys(myId);

You can also do this by : 您也可以通过以下方式执行此操作:

element.sendKeys(Keys.chord(Keys.CONTROL, "a"));
element.sendKeys(Keys.chord(Keys.CONTROL, "c"));
element2.sendKeys(Keys.chord(Keys.CONTROL, "v"));

Maybe this is what you are looking for. 也许这就是您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM