有没有办法从链接的 xpath 获取 JavaScript 中链接的目的地?

发布于 2024-11-06 21:40:50 字数 432 浏览 1 评论 0原文

我正在使用 Selenium RC 为至少部分动态生成或依赖于数据库的网站编写测试用例。我希望能够告诉我的 Python 程序调用 Selenium 对特定链接的目标执行字符串操作,但我无法确定目标(href='' 字段)将位于“编译时间”(或者,好吧,无论 Python 的等价物是什么。有人可以帮助我吗?)。

现在,我确定该链接的 xpath 是什么; "//table[@id='搜索结果']/tbody/tr[2]/td[3]/a/img" .我正在运行 Selenium,它有一个 eval() 函数(万恶之源),可以运行任意 JavaScript 行。有什么方法可以从 xpath 和 Selenium 获取此链接的目标,或者我是否必须找到某种方法来下载整个页面源代码(我该怎么做?应该有一个 Selenium 命令来下载整个页面源代码,但当我尝试运行它时出现“元素不存在”错误),然后打破正则表达式来弄乱整个页面源代码?

I'm using Selenium RC to write test cases for a website that's at least partially dynamically generated or otherwise dependent on a database. I'd like to be able to tell my Python program making calls to Selenium to do string operations on the target of a specific link, but I can't be sure what the target (the href='' field) will be at "compile time" (or, well, whatever the Python equivalent is. Can someone help me out there?).

Now, I am sure what the xpath of that link is going to be; "//table[@id='search-results']/tbody/tr[2]/td[3]/a/img" . And I am running Selenium, which has an eval() function (the root of all evil) that runs arbitrary lines of javascript. Is there any way I can get the target of this link from the xpath and Selenium, or do I have to find some way to download the entire page source (how do I do this? There was supposed to be a Selenium command to download the entire page source, but I got an "element doesn't exist" error when I tried running it) and then break out regexes to mess with the entire page source?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对风讲故事 2024-11-13 21:40:50

我认为您可以使用 get_attribute Python 方法执行此操作:http://code.google.com/p/selenium/source/browse/trunk/py/selenium/selenium.py#1343

该调用看起来像:

href = mySelenium.get_attribute("//table[@id='search-results']/tbody/tr[2]/td[3]/a@href")

I think you can do this using the get_attribute Python method here: http://code.google.com/p/selenium/source/browse/trunk/py/selenium/selenium.py#1343

The call would look something like:

href = mySelenium.get_attribute("//table[@id='search-results']/tbody/tr[2]/td[3]/a@href")
吻安 2024-11-13 21:40:50

您的 XPath 表达式选择一个 img 元素,但在我看来,您想要获取其父 a 元素。因此,如果您的 XPath 正确,那么链接目标应该是:

<...value-of select="//table[@id='search-results']/tbody/tr[2]/td[3]/a@href"/>

或者也许:

<...for-each select="//table[@id='search-results']/tbody/tr[2]/td[3]/a">
  <...value-of select="@href" />
</...for-each>

where ... 是您的命名空间

Your XPath expression selects an img element, but it seems to me that you want to get at its parent a element. So if your XPath is correct, then the link target should be:

<...value-of select="//table[@id='search-results']/tbody/tr[2]/td[3]/a@href"/>

or perhaps:

<...for-each select="//table[@id='search-results']/tbody/tr[2]/td[3]/a">
  <...value-of select="@href" />
</...for-each>

where ... is your namespace

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文