如何在Selenium Webdriver中使用css或xpath获取验证码

时间:2015-08-15 17:33:23

标签: css selenium xpath

<body>
<div class="row-fluid">
<div class="span12">
<div class="mailview" style="margin-right:18px;">
<p>Dear  MohanNimmala First,</p>
<p>Thank you for registering with MediAngels!</p>
<p>
<p>
Verification Code:
<b> 95527</b>

有人可以帮助我从上面获取验证码95527 使用xpath还是css?

我正在使用以下xpath = html/body/div[1]/div/div/p[4]/b

enter image description here

2 个答案:

答案 0 :(得分:0)

我认为,如果您嵌套“验证码”,那么您以后就会遇到问题。如此深入的HTML。当有人更改布局/ html时,测试很可能会失败。

我建议给b标签一个ID并在你的选择器中使用该ID。你的测试会更有弹性。

将HTML属性添加到b-tag:

Verification Code:
<b id="verification-code"> 95527</b>
....

和xpath

//*[@id='verification-code']

你的xpath的问题可能是你在开始时错过了一个斜杠。一个斜杠从根开始,两个斜杠从任何地方开始。

/html/body/div/div/div/p[4]/b

答案 1 :(得分:0)

感谢Deef&amp; Har,对于你的答案,下面的代码可以从innterhtml标签中提取价值

@Test public void test() { SoftAssert sf = new SoftAssert(); Integer[] list = {1, 2, 3}; for(int i : list) sf.assertEquals(3, i, i + " does not equal 3\n"); sf.assertAll(); }