gpt4 book ai didi

selenium - 在 Selenium IDE 中,Locator 可以使用 XPath 和 storedVars

转载 作者:行者123 更新时间:2023-12-01 01:29:18 24 4
gpt4 key购买 nike

我有一个 XPath 可以选择我的特定单选按钮项
HTML。我想通过使用
存储变量来表示我的 XPath 用于的文本
锁上。

这是我目前的设置

命令:点击

目标:

//*[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>

模拟 Selenium 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>

最佳答案

您需要在变量周围加上引号。

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

是的,我已经成功地使用了带有存储变量的 XPath 定位器,尽管它不是通过“点击”而是通过“waitForXpathCount”。起初它不起作用,我不得不求助于 printf 调试来找到原因。它不起作用的原因在这里无关紧要(我没有适本地设置目标),但我认为相关文件列表对那些在使用 Selenium IDE 时遇到问题的人会很感兴趣。

Mac 上文件的位置:

~/Library/Application Support/Firefox/Profiles/[your profile]/extensions/{a6fd85ed-e919-4a43-a5af-8da18bda539f}/chrome/content/selenium-core/scripts/



重要文件:
  • selenium-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");

    每次更改 Selenium IDE 的内部结构时都需要重新启动 Firefox(我找不到即时重新加载它的方法)。

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

    关于selenium - 在 Selenium IDE 中,Locator 可以使用 XPath 和 storedVars,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5825908/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com