<div class="content-wrapper" style="min-height: 611px;">
<div class="alert alert-success alert-dismissable">
<button class="close" type="button" data-dismiss="alert" aria-hidden="true">×</button>
The new customer role has been added successfully.
</div>
如何在Button标记后获取此Text。我尝试了很多方法,但我无法得到它。
//div[@class='alert alert-success alert-dismissable']
答案 0 :(得分:3)
您可以使用//div[@class='alert alert-success alert-dismissable']/text()
xpath找到文本The new customer role has been added successfully.
但是selenium在xpath中不支持text()
方法来定位文本节点。
尝试使用以下代码从
<button>
代码获取文字。
String button = driver.findElement(By.xpath("//button[@class='close'][@type='button']")).getText();
System.out.println(button);
尝试下面的代码来获取文字
The new customer role has been added successfully.
JavascriptExecutor js = (JavascriptExecutor)driver;
Object str = js.executeScript("var value = document.evaluate(\"//button[@class='close']/following::node()[contains(., 'The new customer role has been added successfully.')]\",document, null, XPathResult.STRING_TYPE, null); return value.stringValue;");
System.out.println(str.toString());
您可以详细探索from here
参考下图以供参考。
答案 1 :(得分:0)
我不确定您的HTML,但您可以尝试使用此XPath
//*[@class='alert alert-success alert-dismissable']
使用java getText()
将从html
driver.findElement(By.xpath(" //*[@class='alert alert-success alert-dismissable']")).getText();
让我知道XPath是否有效
答案 2 :(得分:0)
我认为您已尝试使用xpath以下
//*[@class='alert alert-success alert-dismissable']
请您检查一下警告框(我假设它是来自html的警告框)是否会弹出新窗口。在这种情况下,您需要切换到新窗口。
同时检查警报弹出窗口是否有覆盖属性