2011-04-28 43 views
3

我有一个XPath,它将选择我的 HTML中的特定单选按钮项。我想通过使用 存储的变量来表示我的XPath正在使用的文本锁定为 ,从而使我的定位器更加具有动态性。在Selenium IDE中,Locator可以使用XPath和storedVars

这是我的当前设置

命令:点击

目标:

//*[contains(@for,'Page149.Question48.TypeChoiceOneAnswerRadioButton.holder_ctl02') 
and text()="Below 70 – May be too low"] 

我想替换文字文本“低于70 - 可能太低“ 与一个变量。我提议的目标未能评估。我希望我 只是语法不正确。

我可以这样做吗?我可以在XPath定位器中使用存储的变量吗?

提议的目标:

//*[contains(@for,'Page149.Question48.TypeChoiceOneAnswerRadioButton.holder_ctl02') 
and text()=${radioText}] 

编辑:添加代码。我做了一个简单的HTML单选按钮列表示例,并隔离了我的Selenium命令。我在Firefox中使用Selenium IDE。我的命令示例有评论。

样本HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title>Untitled Page</title> 
</head> 
<body> 
    <table border="0" class="srvy_radiobuttonlist" id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02"> 
     <tbody> 
      <tr> 
       <td> 
        <input type="radio" checked="checked" value="Page51.Question5.Answer580" name="ctl00$cphSurveyControls$surveyCategoryPage$Page51.Question5.TypeChoiceOneAnswerRadioButton.holder$ctl02" 
         id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0"><label 
          for="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0">Male</label> 
       </td> 
       <td> 
        <input type="radio" value="Page51.Question5.Answer581" name="ctl00$cphSurveyControls$surveyCategoryPage$Page51.Question5.TypeChoiceOneAnswerRadioButton.holder$ctl02" 
         id="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_1"><label 
          for="ctl00_cphSurveyControls_surveyCategoryPage_Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_1">Female</label> 
       </td> 
      </tr> 
     </tbody> 
    </table> 
</body> 
</html> 

嘲笑了硒IDE

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head profile="http://selenium-ide.openqa.org/profiles/test-case"> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<link rel="selenium.base" href="http://localhost" /> 
<title>LocatorTest</title> 
</head> 
<body> 
<table cellpadding="1" cellspacing="1" border="1"> 
<thead> 
<tr><td rowspan="1" colspan="3">LocatorTest</td></tr> 
</thead><tbody> 
<!--Select Male--> 
<tr> 
    <td>click</td> 
    <td>//*[contains(@id,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02_0')]</td> 
    <td></td> 
</tr> 
<tr> 
    <td>store</td> 
    <td>Female</td> 
    <td>Gender</td> 
</tr> 
<tr> 
    <td>assertTextPresent</td> 
    <td>${Gender}</td> 
    <td></td> 
</tr> 
<tr> 
    <td>assertTextPresent</td> 
    <td>javascript{ storedVars['Gender']}</td> 
    <td></td> 
</tr> 
<!--FYI, locates first item in radio collection--> 
<tr> 
    <td>click</td> 
    <td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02')]</td> 
    <td></td> 
</tr> 
<!--Works to select female--> 
<tr> 
    <td>click</td> 
    <td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and text()='Female']</td> 
    <td></td> 
</tr> 
<tr> 
    <td>click</td> 
    <td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and contains(text(),'Female')]</td> 
    <td></td> 
</tr> 
<!---- not found --> 
<tr> 
    <td>click</td> 
    <td>//*[contains(@for,'Page51.Question5.TypeChoiceOneAnswerRadioButton.holder_ctl02') and contains(text(),${Gender})]</td> 
    <td></td> 
</tr> 
<!--Works to select female--> 
<tr> 
    <td>click</td> 
    <td>//*[contains(text(),'Female')]</td> 
    <td></td> 
</tr> 
<!---- not found --> 
<tr> 
    <td>click</td> 
    <td>//*[contains(text(),${Gender})]</td> 
    <td></td> 
</tr> 
</tbody></table> 
</body> 
</html> 

回答

0

什么是您选择的HTML元素?你能提供HTML吗?是的,这是可能的。你使用什么绑定?即。 Python,Ruby,Java?

你可以做这样的事情:

var = selenium.get_eval(locator) 
0

是的,这应该只是罚款。该documentation显示了一个简单的例子:

type | textElement | Full name is: ${fullname} 

最明显的问题是“你从哪里得到radioText的价值?”,由紧随其后“你确定在它里面没有其他的文字吗?” (因为你编码“text()='xxx'”,而不是“contains(text(), '...')”。或许你真的离开了在代码中的引号,不只是在你的例子(text()=xxx”,而不是“text()='xxx'”)?

+0

我添加了代码示例以帮助说明。您的意见不清楚您的意思。 – MADCookie 2011-04-29 19:28:37

+0

我用Selenium IDE 1.0.10在Firefox 3.6.17上运行了你的测试。它成功运行。日志不适合评论,但这里是有用的部分。 #[info]执行:| store |女性|性别| #[info]执行:| assertTextPresent | $ {Gender} | | #[info]执行:| assertTextPresent | javascript {storedVars ['Gender']} | | 然后我将“女性”改为“香蕉”,并得到这个: #[info]执行:| store |香蕉|性别| #[info]执行:| assertTextPresent | $ {Gender} | | #[错误]假 – 2011-05-04 18:35:25

4

你需要引号的变量。

//*[contains(text(),'${Gender}')] 

是的,我已经成功地使用一个XPath定位器与存储变量,虽然这不是与“点击”,但与“waitForXpathCount”。起初,它没有工作我不得不求助于printf调试来找到原因。它没有工作的原因在这里无关(我没有适当地设置目标),但我认为有关文件的列表将对那些在Selenium IDE中遇到问题的人感兴趣。

位置在Mac上的文件:

〜/库/ Application Support /火狐/概况/ [个人资料] /扩展/ {a6fd85ed-e919-4a43-a5af-8da18bda539f} /镀铬/内容/硒核心/脚本/

重要文件:

  • 硒api.js
    核心命令处理程序。恩。 Selenium.prototype.doClick
  • selenium-browserbot.js
    负责连接浏览器。例如,上面的doClick处理程序调用BrowserBot.prototype.findElement
  • selenium-executionloop.js
    调用命令处理程序。在这种情况下最重要的是,预处理定位符字符串为selenium.preprocessParameter(command.target)selenium.preprocessParameter可在selenium-api.js中找到。
  • htmlutils.js
    提供XPathEvaluator

如何打印到IDE日志:

LOG.error("Useful debugging information"); 

您需要重新启动Firefox每次进行更改硒IDE的内部时间(我无法找到一种方式来重新加载它)。

另外,您可以在普通的Firefox JS控制台上测试您的XPath表达式,如$x("xpath/expr/with/stored/variables/replaced/accordingly")

+0

后续代码为我工作: '// * [包含(文本(), '$ {}性别')]' 我的具体的解决办法是: '//一个[包含(text(),'$ {categoryName}')]/following-sibling :: a' – Campinho 2012-11-07 13:06:21

相关问题