css - Selenium 无法识别页面上第二个元素的 CSS 路径

标签 css xpath selenium css-selectors

我正在尝试使用 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/

相关文章:

javascript - 如何设置 React 渲染组件的样式

javascript - 为什么 ../file 路径在 css 和 html 中不起作用?

html - XPath/XSLT 删除空标签

python - 在 Windows 8 上安装并运行 Django

java - Selenium 网络驱动程序 : How many times does a driver try to find element with an implicit wait timeout?

javascript - 多次使用 MySQLi 查询结果数组

javascript - 将左侧面板动态附加到所有页面

html - 识别文本出现次数最少的元素的正确 XPath 是什么?

c# - xpath 定位器 c# 正确的查找技术

selenium - 标签后的机器人框架输入文本