我正在尝试使用 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路径区分这两个链接?
最佳答案
使用 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)
关于css - Selenium 无法识别页面上第二个元素的 CSS 路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6253262/