【发布时间】:2015-07-01 11:06:46
【问题描述】:
我在为按钮找到正确的 Xpath 语法时遇到了一些麻烦。 页面上有 2 个添加按钮。报告下方有一个添加按钮。 项目下方有一个添加按钮。 我想在 Projects 下面找到那个,但我在 Reports 下面找到那个。
我尝试了以下 Xpaths,这些找到了 span 类 Reports 下方的添加按钮:
//button[div[. = 'Add...']]
//div[@class="gwt-HTML" and contains(text(), "Add...")]
e.g. driver.find_element_by_xpath(//button[div[. = 'Add...']])
我想在跨度类项目下方找到添加...按钮。 我可以使用以下 Xpath 找到 Projects span 类。我不知道如何继续 Xpath 并找到 Add... 按钮。
//span[@class="gwt-InlineLabel" and contains(text(), 'Projects')]
HTML sn-p 如下:
<div>
<div style="position: absolute; top: 3px; left: 5px;">
<span class="gwt-InlineLabel" style="font-weight: bold; color: black; margin-right: 5px; margin-left: 20px;">Reports</span>
<div class="GAT4PNUMP">
<button class="gwt-Button" type="button">
<div style="position: absolute; margin-left: 2px;">
<div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
</button>
<button class="gwt-Button" type="button" disabled="">
<button class="gwt-Button" type="button" disabled="">
<div class="GAT4PNUMP">
<button class="gwt-Button" type="button" title="Run the selected reports" disabled="">
<button class="gwt-Button" type="button" title="View the selected report" disabled="">
<button class="gwt-Button workspacebuttontext130" type="button" title="Conditional report" disabled="">Conditional report ▼</button>
<div class="GAT4PNUMP">
<select class="gwt-ListBox" style="height: 20px; margin-right: 5px;" disabled="">
</div>
<div>
<div style="position: absolute; top: 3px; left: 5px;">
<span class="gwt-InlineLabel" style="font-weight: bold; color: black; margin-right: 5px; margin-left: 20px;">Projects</span>
<div class="GAT4PNUMP">
<button class="gwt-Button" type="button">
<div style="position: absolute; margin-left: 2px;">
<div class="gwt-HTML" style="margin-left: 15px;">Add...</div>
</button>
<button class="gwt-Button" type="button" disabled="">
<button class="gwt-Button workspacebuttontext75" type="button" title="Delete" disabled="">Delete </button>
<div class="GAT4PNUMP">
<button class="gwt-Button workspacebuttontext60" type="button" title="Import to the selected project" disabled="">Import...</button>
<button class="gwt-Button workspacebuttontext60" type="button" title="Export from the selected project" disabled="">Export...</button>
<button class="gwt-Button workspacebuttontext75" type="button" title="Upgrade the selected project" disabled="">Upgrade...</button>
<button class="gwt-Button workspacebuttontext60" type="button" title="Refresh the selected project" disabled="">Refresh</button>
</div>
我可以使用什么 Xpath 来定位位于跨度类 Projects 下方的 Add...? 谢谢。
【问题讨论】:
-
如果您使用 Firefox 浏览器,您可以安装 Selenium IDE 插件并简单地获取所需页面元素的唯一选择器(Xpath、css_selector...)
-
我安装了 IDE 2.9.0。 IDE 中的哪里是唯一选择器的选项?如果我在目标字段中记录点击,我可以在下拉 xpath=(//button[@type='button'])[35] 中看到。这是选择器吗?
-
嗯,是的。你可以用它来按下你的按钮
driver.find_element_by_xpath("(//button[@type='button'])[35]").click()
标签: python selenium xpath selenium-webdriver