Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在Selenium中区分这两个按钮?_Python_Html_Selenium - Fatal编程技术网

Python 如何在Selenium中区分这两个按钮?

Python 如何在Selenium中区分这两个按钮?,python,html,selenium,Python,Html,Selenium,我在Python上运行Selenium,我不知道如何区分这些按钮,以便使用Selenium单击其中一个按钮。我需要点击选项1。我不知道如何从它们身上获取任何重要的数据标签,但我对此非常陌生 <tbody> <tr> <td>TOP1 </td> <td>11/16/15</td> <td>12/

我在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>

选项1、3和4不起作用,因为它们不是正确的xpath(您没有
title
属性,
button
不是类,
Option\u 1
不是按钮文本的一部分);选项2将返回第一个元素,因此它应该适用于页面上的第一个按钮,但通常最好更具体一些

如果按钮的文本不同(它不在您的HTML代码中,但我不确定它不是打字错误),则使用:

//button[contains(text(),'Add Alert')]
否则使用

//button[contains(@onclick,'option_1')]
如果在
中使用这两个xpath都失败,请按如下方式查找:

driver.find_element_by_xpath(...).click()
然后你需要检查两件事:

  • 这个表单是在某种iframe中吗?如果是,你需要在找到它之前

  • 也可能是按钮没有立即出现,您需要:

    wait=WebDriverWait(驱动程序,10)
    element=wait.until(EC.element可点击((By.XPATH,“”))
    元素。单击()
    

  • 您尝试的那些与标记中的内容不匹配,首先您需要为按钮添加类,如果您想使用@class

    但是您所做的仍然是错误的,如果这个表是动态的,那么您需要使用List来获取表中的行。首先为表格行和按钮添加类:

    <!-- Add class to your <tr> -->
    <tr class="rows">
    <!-- and class to you <button> -->
    <button class="button" onclick="addNotify('RANDOMNUMBERS','option_1    ','Option1                                 ','IDNUMBER');">Add Alert</button>
    
    
    添加警报
    
    用途:

    List<WebElement> buttons = driver.findElements(By.cssSelector(".rows"));
    //Since you wanted the first button after TOP 1 use get index 0 (get(0))
    buttons.get(0).findElement(By.xpath("//button[@class='button']")).click();
    
    List buttons=driver.findElements(By.cssSelector(“.rows”);
    //因为您需要前1名之后的第一个按钮,所以请使用get index 0(get(0))
    buttons.get(0).findElement(By.xpath(//button[@class='button']))。单击();
    
    您能用代码本身而不是屏幕截图来编辑这个问题吗?我已经解决了。//button[contains(@onclick,'option_1')]成功了,谢谢!我在放@value,我没意识到@value就是html标签!
    <!-- Add class to your <tr> -->
    <tr class="rows">
    <!-- and class to you <button> -->
    <button class="button" onclick="addNotify('RANDOMNUMBERS','option_1    ','Option1                                 ','IDNUMBER');">Add Alert</button>
    
    List<WebElement> buttons = driver.findElements(By.cssSelector(".rows"));
    //Since you wanted the first button after TOP 1 use get index 0 (get(0))
    buttons.get(0).findElement(By.xpath("//button[@class='button']")).click();