【问题标题】:Selenium does not recognize the CSS path for a second element on the pageSelenium 无法识别页面上第二个元素的 CSS 路径
【发布时间】:2011-09-09 08:14:04
【问题描述】:

我正在尝试使用 CSS 选择器单击 Selenium 中的链接,但它无法识别它。

名为“取消”的链接出现在页面的两个位置。

第一个取消链接的 xpath 是: //html/body/div[@id='c_account']/div[@id='a_returns']/div[@id='container']/div[@id='main']/div[@id='main_col']/div/div[@id='create-return']/div[1]/div/a[2]/span

第二个链接的 xpath 是: /html/body/div[@id='c_account']/div[@id='a_returns']/div[@id='container']/div[@id='main']/div[@id='main_col']/div/div[@id='create-return']/div[4]/div/a[2]/span

当我在 Firebug 中检查两个链接时,我得到了相同的 CSS 路径。 html body.p div#c_account.c_wrapper div#a_returns.a_wrapper div#container div#main div#main_col div.main_content div#create-return div.return-process-actions div.return-process-action-buttons a.return-process-cancel

当我使用 Selenium 单击第二个链接时,它会单击第一个链接,因为两者的 css 路径相同。

有没有办法通过css路径区分两个链接?

【问题讨论】:

    标签: css xpath selenium css-selectors


    【解决方案1】:

    使用 x:nth-child(n)

    查找此站点:http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ ...您需要的是 #22

    另外,你可能真的不需要这么长的 css 选择器,你可以直接使用

    
    a.return-process-cancel:nth-child(1)  
    a.return-process-cancel:nth-child(2)
    

    【讨论】:

      【解决方案2】:

      从长远来看,您最好为要单击的链接添加一个具有有意义名称的id。当你有类似的行时,你的测试以后会更容易阅读

      selenium.click("firstCancelLink");
      

      selenium.click("css=html body.p div#c_account.c_wrapper div#a_returns.a_wrapper div#container div#main div#main_col div.main_content div#create-return div.return-process-actions div.return-process-action-buttons a.return-process-cancel");
      

      【讨论】:

      • 是的,但这不是我正在寻找的解决方案,因为我无法更改开发人员编写的代码。
      猜你喜欢
      • 2020-08-16
      • 2013-09-20
      • 1970-01-01
      • 2017-09-13
      • 2019-01-12
      • 1970-01-01
      • 2013-03-02
      • 1970-01-01
      • 2020-06-20
      相关资源
      最近更新 更多