有没有办法从链接的 xpath 获取 JavaScript 中链接的目的地?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您可以使用
get_attribute
Python 方法执行此操作:http://code.google.com/p/selenium/source/browse/trunk/py/selenium/selenium.py#1343该调用看起来像:
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#1343The call would look something like:
您的 XPath 表达式选择一个 img 元素,但在我看来,您想要获取其父 a 元素。因此,如果您的 XPath 正确,那么链接目标应该是:
或者也许:
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:
or perhaps:
where ... is your namespace