在 Selenium 中导航表格

发布于 2024-08-20 16:16:07 字数 221 浏览 3 评论 0原文

假设我有一个简单的表,如下所示:

Smith    |  Select
Jones    |  Select
Johnson  |  Select

我需要编写一个 Selenium 测试来选择与 Jones 对应的链接。我可以让它单击第二行中的“选择”,但我宁愿让它找到“琼斯”所在的位置并单击相应的“选择”。我想我可能需要合并 JQuery 来完成这个任务?

Suppose I have a simple table, like this:

Smith    |  Select
Jones    |  Select
Johnson  |  Select

And I need to write a Selenium test to select the link corresponding to Jones. I can make it click the "Select" in the 2nd row, but I would rather have it find where "Jones" is and click the corresponding "Select". I'm thinking I may need to incorporate JQuery to accomplish this?

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

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

发布评论

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

评论(2

走野 2024-08-27 16:16:07

使用类似“//tc[.//text() = 'Jones']/../tc[2]/a”的 xpath 表达式,查找文本内容为 'Jones' 的表格单元格,然后导航到该单元格的父级,选择该父级的第二个表格单元格,然后选择第二个表格单元格内的链接。

Use an xpath expression like "//tc[.//text() = 'Jones']/../tc[2]/a" which looks for a table cell whose text contents are 'Jones' and then naviagates up to that cells parent, selects the second table cell of that parent, and then a link inside the second table cell.

倚栏听风 2024-08-27 16:16:07

如果您想使用 jQuery,这里有一些信息:

  • 首先,您可以从 jquery.js 或 jquery.min.js 文件读取 jquery。
  • 然后使用execute_script(jquery)动态启用jquery。
  • 现在您可以与 jquery 交互。

这是一些代码:

browser = webdriver.Firefox() # Get local session of firefox

with open('jquery.min.js', 'r') as jquery_js: #read the jquery from a file
    jquery = jquery_js.read()
    browser.execute_script(jquery)  #active the jquery lib

#now you can write some jquery code then execute_script them
js = """
    var str = "div#myPager table a:[href=\\"javascript:__doPostBack('myPager','%s')\\"]"
    console.log(str)
    var $next_anchor = $(str);
    if ($next_anchor.length) {
        return $next_anchor.get(0).click(); //do click and redirect
    } else {
        return false;
    }""" % str(25) 

success = browser.execute_script(js)
if success == False:
    break

If you wanna use jQuery, here is some information:

  • First you can read the jquery from a jquery.js or jquery.min.js file.
  • Then using execute_script(jquery) to enable jquery dynamically.
  • Now you can interact with jquery.

here is some code:

browser = webdriver.Firefox() # Get local session of firefox

with open('jquery.min.js', 'r') as jquery_js: #read the jquery from a file
    jquery = jquery_js.read()
    browser.execute_script(jquery)  #active the jquery lib

#now you can write some jquery code then execute_script them
js = """
    var str = "div#myPager table a:[href=\\"javascript:__doPostBack('myPager','%s')\\"]"
    console.log(str)
    var $next_anchor = $(str);
    if ($next_anchor.length) {
        return $next_anchor.get(0).click(); //do click and redirect
    } else {
        return false;
    }""" % str(25) 

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