如何使用 Selenium 和 Java 从 html 中提取文本 2?
How to extract the text 2 from the html using Selenium and Java?
将 Java 与 Selenium 和 Eclipse 以及 Chrome 结合使用。我在 Chrome 的页面上,我想找到一个 xpath,所以我进行了检查,然后右键单击->复制->xpath,它返回了以下内容:
//*[@id="queueForm"]/div[1]/span[3]/text()[2]
所以我做了一个
// driver is an instance of ChromeDriver
String xp = "//*[@id="queueForm"]/div[1]/span[3]/text()[2]";
WebElement ele = driver.findElement(By.xpath(xp));
并且我收到一条错误消息,它返回了一个对象而不是 WebElement。有人 运行 遇到过这种情况吗? HTML 就像
<span class="displayOption" style="float:right;">
Page <input id="gotoresultpage" type="text" onkeypress="javascript:fncCaptureEnter(this);" size="1" maxlength="4" tabindex="1" value="1" class="navigationMenu" style="width:20px;text-align:right"> of 2
<input type="button" tabindex="1" value="Go" id="pageGo" name="pageGo" class="navigationButton" onclick="g_objQueueUiController.fncGo();">
<span class="recordNavigationBack"></span>
<span class="recordNavigationForwardEnabled" id="pageNext" name="pageNext" onclick="g_objQueueUiController.fncGoToPage('2')"></span>
</span>
您可以在其中看到文本 "Page",稍后 "of 2"。页码在输入框中。
所以我正在尝试获取 "of 2" 文本。我已经从输入框的值中得到了页码,但不知道如何得到 "of 2".
试试这个
xpath = //span[@class='displayOption']//input[@id='gotoresultpage']
driver.findElement(By.xpath(xpath))).getAttribute("value");
从 XPath 表达式的末尾删除此 text()
,从 WebDriver.find() function must be a Node 返回的值,而不是文本。
因此您需要将代码修改为:
//input[@id='gotoresultpage']
示例代码:
WebElement someElement = driver.findElement(By.xpath("//input[@id='gotoresultpage']"))
String text = someElement.getText();
更多信息:
虽然 运行 Selenium Web 驱动程序,但有时页面在后端加载 DOM 和页面本身会花费更多时间,这会导致 Web 驱动程序无法通过测试步骤.尝试在您的代码中使用 'Waits',以便它提供更多时间来加载您的页面和 DOM。
尝试使用此代码等待:
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
或者简单地使用 thread.sleep:
Thread.sleep(长毫秒,整数纳米);
(示例:Thread.sleep(5000、50000);这将在您的测试步骤之间实施 5 秒休眠)
此外,请注意自动生成的 xpath 是动态的,它可能会在您下一次执行测试脚本时更新,因此在选择要通过网络驱动程序找到的网络元素时要保持警惕。
文本of 2是一个文本节点,所以要提取文本你需要诱导WebDriverWait 用于 visibility_of_element_located()
并且您可以使用以下任一项 :
使用cssSelector
:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#queueForm>div span.displayOption[style*='right']")))).toString());
使用xpath
:
System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id="queueForm"]/div//span[@class='displayOption' and contains(@style, 'right')]")))).toString());
我做了如下测试 link : https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_form
这是HTML
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="lname" form="form1">
</body>
</html>
我得到了形式
的文本"First name"
这是我的代码
driver.switchTo().frame("iframeResult");
driver.findElement(By.id("form1")).getText();
最后一行return"First name"
将 Java 与 Selenium 和 Eclipse 以及 Chrome 结合使用。我在 Chrome 的页面上,我想找到一个 xpath,所以我进行了检查,然后右键单击->复制->xpath,它返回了以下内容:
//*[@id="queueForm"]/div[1]/span[3]/text()[2]
所以我做了一个
// driver is an instance of ChromeDriver
String xp = "//*[@id="queueForm"]/div[1]/span[3]/text()[2]";
WebElement ele = driver.findElement(By.xpath(xp));
并且我收到一条错误消息,它返回了一个对象而不是 WebElement。有人 运行 遇到过这种情况吗? HTML 就像
<span class="displayOption" style="float:right;">
Page <input id="gotoresultpage" type="text" onkeypress="javascript:fncCaptureEnter(this);" size="1" maxlength="4" tabindex="1" value="1" class="navigationMenu" style="width:20px;text-align:right"> of 2
<input type="button" tabindex="1" value="Go" id="pageGo" name="pageGo" class="navigationButton" onclick="g_objQueueUiController.fncGo();">
<span class="recordNavigationBack"></span>
<span class="recordNavigationForwardEnabled" id="pageNext" name="pageNext" onclick="g_objQueueUiController.fncGoToPage('2')"></span>
</span>
您可以在其中看到文本 "Page",稍后 "of 2"。页码在输入框中。
所以我正在尝试获取 "of 2" 文本。我已经从输入框的值中得到了页码,但不知道如何得到 "of 2".
试试这个
xpath = //span[@class='displayOption']//input[@id='gotoresultpage']
driver.findElement(By.xpath(xpath))).getAttribute("value");
从 XPath 表达式的末尾删除此 text()
,从 WebDriver.find() function must be a Node 返回的值,而不是文本。
因此您需要将代码修改为:
//input[@id='gotoresultpage']
示例代码:
WebElement someElement = driver.findElement(By.xpath("//input[@id='gotoresultpage']"))
String text = someElement.getText();
更多信息:
虽然 运行 Selenium Web 驱动程序,但有时页面在后端加载 DOM 和页面本身会花费更多时间,这会导致 Web 驱动程序无法通过测试步骤.尝试在您的代码中使用 'Waits',以便它提供更多时间来加载您的页面和 DOM。 尝试使用此代码等待:
WebDriverWait wait = new WebDriverWait(driver, 15); wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
或者简单地使用 thread.sleep: Thread.sleep(长毫秒,整数纳米);
(示例:Thread.sleep(5000、50000);这将在您的测试步骤之间实施 5 秒休眠)
此外,请注意自动生成的 xpath 是动态的,它可能会在您下一次执行测试脚本时更新,因此在选择要通过网络驱动程序找到的网络元素时要保持警惕。
文本of 2是一个文本节点,所以要提取文本你需要诱导WebDriverWait 用于 visibility_of_element_located()
并且您可以使用以下任一项
使用
cssSelector
:System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#queueForm>div span.displayOption[style*='right']")))).toString());
使用
xpath
:System.out.println(((JavascriptExecutor)driver).executeScript('return arguments[0].childNodes[3].textContent;', new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id="queueForm"]/div//span[@class='displayOption' and contains(@style, 'right')]")))).toString());
我做了如下测试 link : https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_input_form
这是HTML
<!DOCTYPE html>
<html>
<body>
<form action="/action_page.php" id="form1">
First name: <input type="text" name="fname"><br>
<input type="submit" value="Submit">
</form>
<p>The "Last name" field below is outside the form element, but still part of the form.</p>
Last name: <input type="text" name="lname" form="form1">
</body>
</html>
我得到了形式
的文本"First name"这是我的代码
driver.switchTo().frame("iframeResult");
driver.findElement(By.id("form1")).getText();
最后一行return"First name"