我正在 Python 上运行 Selenium,但我不知道如何区分这些按钮,以便我可以使用 Selenium 单击其中一个。我需要单击选项 1。我不知道如何从中获取任何重要的数据标签,但我对此非常陌生!
<tbody>
<tr>
<td>TOP1 </td>
<td>11/16/15</td>
<td>12/30/99</td>
<td>Balance due on account</td>
<td><button onclick="addNotify('RANDOMNUMBERS','option_1 ','Option1 ','IDNUMBER');">Add Alert</button></td>
</tr>
<tr>
<td>TOP2 </td>
<td>11/16/15</td>
<td>12/30/99</td>
<td>Balance due on account</td>
<td><button onclick="addNotify('RANDOMNUMBERS','option_2 ','Option2 ','IDNUMBER');">Add Alert</button></td>
</tr>
</tbody>
我需要点击 TOP1 后面的按钮的“添加提醒”
我已经尝试了所有这些(单独)以及更多,但无法让任何一个为我工作:
driver.find_element_by_xpath("//button[@title='Add Hold']").click()
driver.find_element_by_tag_name("button").click()
driver.find_element_by_xpath("//div[@class='button' and contains(text(), 'Add Alert')]").click()
driver.find_element_by_xpath("//*[@class=\"button\"]/descendant::span[text()='option_1 ']").click()
最佳答案
选项 1、3 和 4 不起作用,因为它们不是正确的 xpath(您没有 title
属性,button
不是一个类,并且 option_1
不是按钮文本的一部分);选项 2 将返回第一个元素,因此它应该适用于页面上的第一个按钮,但通常最好更具体。
如果按钮的文本不同(它不在您的 HTML 代码中,但我不确定它是否有拼写错误),则使用:
//button[contains(text(),'Add Alert')]
否则使用
//button[contains(@onclick,'option_1')]
如果这两个 xpath 在 find
中使用时都失败,如下所示:
driver.find_element_by_xpath(...).click()
那么你需要检查两件事:
此表单是否位于某种 iframe 中?如果是,您需要 switch to iframe在找到之前
也可能是按钮没有立即出现,您需要 wait对于它:
wait = WebDriverWait(driver, 10) element = wait.until(EC.element_to_be_clickable((By.XPATH,"<same xpath as above>"))) element.click()
关于python - 我如何在 Selenium 中区分这两个按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37770309/