有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javascript如何从段落中选择大量单词?(硒)

该网页由段落列表组成,当用户单击按钮时,所选单词应从该段落中复制

如何通过硒来实现这一点

我写了javascript代码,但它没有选择单词

String script = "var range = document.createRange();" + "var start = document.getElementsClassname('COTX1');" + "var textNode = start.getElementsByTagName('p')[0].firstChild;" + "range.setStart(textNode, 8);" + "range.setEnd(textNode, 13);" + "window.getSelection().addRange(range);"; ((JavascriptExecutor)driver).executeScript(script);

在下面的段落中:我想选择从早期更具经济可行性的单词

<p class="COTX1"><span class="CO_DC">A</span>s early as 1619, white Americans imported enslaved Africans to the New World with the idea of making agricultural enterprises such as tobacco growing more economically viable. Though most Americans considered it morally wrong, slavery persisted as part of the economic engine that European settlements built on these shores. The Founding Fathers knew that slavery violated the republic’s democratic principles, yet the constitutional debates of 1787–88 show that if it had been outlawed, some colonies would never have joined the union.</p>

共 (1) 个答案

  1. # 1 楼答案

    我认为,可以使用selenium获取完整字符串,并使用regEx完成剩余部分

    WebDriverWait wait = new WebDriverWait(driver, 10);
    String entireString = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("p.COTX1"))).getText();
    String[] strArray = entireString.split("\\.");
    System.out.println(strArray[0]);