我正在尝试打印一条错误消息,出现在页面上的错误消息是:
错误您的请求未成功。您需要联系客户服务来安排更换。
现在,在上面的错误消息中,“联系客户服务”是一个链接,该链接重定向到另一个页面。
我尝试了以下方法来获取文本:
// WebElement text = driver.findElement(By.xpath("//a[contains(text(),'contact Customer Care')]"));
or
// WebElement text = driver.findElement(By.xpath("//p[contains(text(),'Your request has not been successful. You need to')]"));
or
// WebElement text = driver.findElement(By.xpath("//a[@href='/app/contact']"));
or
WebElement text = driver.findElement(By.xpath("//div[@id='SubmitFormError']"));
System.out.println(text.getText());
什么都没用。它没有打印任何内容。
代码是:
<div id="SubmitFormError" class="tab-pane fade in active">
<p></p>
<p id="yui_3_17_2_1_1523943995346_146">Your request has not been successful. You need to
<a href="/app/contact" class="link-dark-bold" id="yui_3_17_2_1_1523943995346_145">contact Customer Care</a> to arrange a replacement.
</p>
<p></p>
</div>
嗨,我只是使用您提供的所有信息从头开始创建了一个项目。如果您执行以下操作(index.html
上面提供的html片段),则似乎可行。请setupPage()
根据您的需要进行调整:
public class MyTest extends Selenide {
@Rule
public ScreenShooter makeScreenshotOnFailure = ScreenShooter.failedTests();
private static WebDriver webDriver;
@Before
public void setupPage() {
Configuration.reportsFolder = "build/reports/integrationTest/screenshots";
Configuration.browser = WebDriverRunner.PHANTOMJS;
DesiredCapabilities caps = new DesiredCapabilities();
caps.setJavascriptEnabled(true);
caps.setCapability(PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "assets/driver/phantomjs_mac");
String[] phantomJsArgs = { "--ignore-ssl-errors=true" };
caps.setCapability(PhantomJSDriverService.PHANTOMJS_GHOSTDRIVER_CLI_ARGS, phantomJsArgs);
caps.setCapability("takesScreenshot", true);
webDriver = new PhantomJSDriver(caps);
webDriver.manage().window().setSize(new Dimension(1280, 800));
WebDriverRunner.setWebDriver(webDriver);
open("file:///Users/UserName/Desktop/index.html");
}
@Test
public void test() {
WebElement text = webDriver.findElement(By.xpath("//div[@id='SubmitFormError']/p[2]/a"));
System.out.println(text.getText());
}
}
就我而言,它可以正确打印:contact Customer Care
。
现在,您可以拨打电话,text.click();
然后您将通过链接进入/app/contact
可以阅读新知识Elements
和做事的地方。我将重命名text
为更有意义的东西。
顺便说一句:我从daveoncode的注释中获得了/ p [2]。
如果这是您在本课程中唯一的测试,请记住webDriver.quit();
在测试结束时添加来退出驱动程序。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句