Selenium Webdriver-Java;查找并单击表中的按钮(带有事件监听器)

时间:2018-08-14 12:54:44

标签: java html selenium selenium-webdriver webdriver

关于我的第一个问题。经过一天的研究,我没有任何进展。

尝试使用NetDriver在Netbeans中测试WebDriver以测试我们的服务,因为我们想在以后测试文件上传。

第1页(有效)

// path of FireFox Driver
    System.setProperty("webdriver.gecko.driver", "C:\\Users\\stevenglasius\\Documents\\WebDriver\\geckodriver.exe");
    WebDriver driverFireFox = new FirefoxDriver();

    // website url
    driverFireFox.get("https://website.nl");
    Thread.sleep(3000);

    // login
    WebElement userName = driverFireFox.findElement(By.id("login:j_idt33"));
    userName.sendKeys("willCost@me.my");
    WebElement passWord = driverFireFox.findElement(By.id("login:j_idt38"));
    passWord.sendKeys("jobIfITellYa");
    WebElement GoToApp = driverFireFox.findElement(By.id("login:j_idt42_label"));
    GoToApp.click();
    Thread.sleep(1000);
    WebElement GoToProductManager = driverFireFox.findElement(By.id("login:j_idt42_7"));
    GoToProductManager.click();
    Thread.sleep(1000);
    WebElement login = driverFireFox.findElement(By.id("login:j_idt47"));
    login.click();
    Thread.sleep(5000);

下一页/ Web应用程序已加载。 我需要单击一个ID为“ menuForm:j_idt823”的按钮

按ID尝试

WebElement upload = driverFireFox.findElement(By.id("menuForm:j_idt823"));
upload.click();

xpath尝试过

WebElement productUpload = driverFireFox.findElement(By.xpath(".//*[@id='menuForm:j_idt823']/table/tbody/tr[4]"));
productUpload.click();

尝试了预期的条件:

WebDriverWait wait = new WebDriverWait(driverFireFox, 10);
...
WebElement productUpload = wait.until(ExpectedConditions.elementToBeClickable(By.id("menuForm:j_idt823")));
productUpload.click();

相同的错误:无法找到元素。 可能是一个问题,有一个事件吗? Thread.sleep(5000);包含在用于加载页面的查找元素之前。 我是编程新手,请帮助我!

HTML event

完整表格:

<tbody>
<tr>
    <td>
        <span style="font-weight: bold">Start Scherm</span>
    </td>
</tr>
<tr>
    <td>
        <button id="menuForm:j_idt821" name="menuForm:j_idt821" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt821&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-bottom:5px !important;margin-top:10px !important;" type="submit" role="button" aria-disabled="false">
            <span class="ui-button-icon-left ui-icon ui-c fa fa-home"/>
            <span class="ui-button-text ui-c">Home</span>
        </button>
    </td>
</tr>
<tr>
    <td>
        <span style="font-weight: bold">Update &amp; Process Uw Data</span>
    </td>
</tr>
<tr>
    <td>
        <button id="menuForm:j_idt823" name="menuForm:j_idt823" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt823&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-bottom:5px !important;margin-top:10px !important;" type="submit" role="button" aria-disabled="false">
            <span class="ui-button-icon-left ui-icon ui-c fa fa-upload"/>
            <span class="ui-button-text ui-c">Product Upload</span>
        </button>
    </td>
</tr>
<tr>
    <td>
        <button id="menuForm:j_idt824" name="menuForm:j_idt824" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt824&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-bottom:5px !important;" type="submit" role="button" aria-disabled="false">
            <span class="ui-button-icon-left ui-icon ui-c fa fa-arrows-alt"/>
            <span class="ui-button-text ui-c">Product Data Management</span>
        </button>
    </td>
</tr>
<tr>
    <td>
        <button id="menuForm:j_idt825" name="menuForm:j_idt825" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left ui-state-disabled button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt825&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-bottom:5px !important;" type="submit" disabled="disabled" role="button" aria-disabled="true">
            <span class="ui-button-icon-left ui-icon ui-c fa fa-upload"/>
            <span class="ui-button-text ui-c">Voorraad Upload</span>
        </button>
    </td>
</tr>
<tr>
    <td>
        <button id="menuForm:j_idt826" name="menuForm:j_idt826" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt826&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-bottom:10px !important;" type="submit" role="button" aria-disabled="false">
            <span class="ui-button-icon-left ui-icon ui-c fa fa-upload"/>
            <span class="ui-button-text ui-c">Prijs Upload</span>
        </button>
    </td>
</tr>
<tr>
    <td>
        <fieldset id="menuForm:j_idt827" class="ui-fieldset ui-widget ui-widget-content ui-corner-all ui-hidden-container ui-fieldset-toggleable ui-hidden-container" style="border: none !important;padding: 0em 0em !important;margin-left: 10px !important;max-width:130px !important;width:130px !important;" data-widget="widget_menuForm_j_idt827">
            <legend class="ui-fieldset-legend ui-corner-all ui-state-default">
                <span class="ui-fieldset-toggler ui-icon ui-icon-plusthick"/>Create &amp; Maintain Uw Eigen Templates</legend>
            <div class="ui-fieldset-content" style="display:none">
                <button id="menuForm:j_idt828" name="menuForm:j_idt828" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt828&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-bottom:5px !important;margin-top:10px !important;" type="submit" role="button" aria-disabled="false">
                    <span class="ui-button-icon-left ui-icon ui-c fa fa-keyboard-o"/>
                    <span class="ui-button-text ui-c">Product File</span>
                </button>
                <button id="menuForm:j_idt829" name="menuForm:j_idt829" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt829&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-bottom:5px !important;" type="submit" role="button" aria-disabled="false">
                    <span class="ui-button-icon-left ui-icon ui-c fa fa-keyboard-o"/>
                    <span class="ui-button-text ui-c">Voorraad File</span>
                </button>
                <button id="menuForm:j_idt830" name="menuForm:j_idt830" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt830&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-bottom:10px !important;" type="submit" role="button" aria-disabled="false">
                    <span class="ui-button-icon-left ui-icon ui-c fa fa-keyboard-o"/>
                    <span class="ui-button-text ui-c">Prijs File</span>
                </button>
            </div>
            <input id="menuForm:j_idt827_collapsed" name="menuForm:j_idt827_collapsed" value="true" type="hidden">
            </fieldset>
        </td>
    </tr>
    <tr>
        <td>
            <fieldset id="menuForm:j_idt831" class="ui-fieldset ui-widget ui-widget-content ui-corner-all ui-hidden-container ui-fieldset-toggleable ui-hidden-container" style="border: none !important;padding: 0em 0em !important;margin-left: 10px !important;max-width:130px !important;width:130px !important;margin-top: 10px !important;" data-widget="widget_menuForm_j_idt831">
                <legend class="ui-fieldset-legend ui-corner-all ui-state-default">
                    <span class="ui-fieldset-toggler ui-icon ui-icon-plusthick"/>Downloads</legend>
                <div class="ui-fieldset-content" style="display:none">
                    <button id="menuForm:j_idt832" name="menuForm:j_idt832" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.monitorDownload(start, stop);;" style="margin-bottom:5px !important;margin-top:10px !important;" type="submit" role="button" aria-disabled="false">
                        <span class="ui-button-icon-left ui-icon ui-c fa fa-download"/>
                        <span class="ui-button-text ui-c">Download Seller Report</span>
                    </button>
                    <button id="menuForm:j_idt833" name="menuForm:j_idt833" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt833&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-bottom:10px !important;" type="submit" role="button" aria-disabled="false">
                        <span class="ui-button-icon-left ui-icon ui-c fa fa-download"/>
                        <span class="ui-button-text ui-c">Download Onetrail Templates</span>
                    </button>
                </div>
                <input id="menuForm:j_idt831_collapsed" name="menuForm:j_idt831_collapsed" value="true" type="hidden">
                </fieldset>
            </td>
        </tr>
        <tr>
            <td>
                <button id="menuForm:j_idt834" name="menuForm:j_idt834" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left button" onclick="PrimeFaces.ab({s:&quot;menuForm:j_idt834&quot;,u:&quot;menuForm tabView&quot;});return false;" style="margin-left:10px !important;margin-top:10px !important;" type="submit" role="button" aria-disabled="false">
                    <span class="ui-button-icon-left ui-icon ui-c fa fa-user"/>
                    <span class="ui-button-text ui-c">User profile</span>
                </button>
            </td>
        </tr>
    </tbody>

2 个答案:

答案 0 :(得分:0)

这应该做到。

WebElement upload = driverFireFox.findElement(By.xpath("//button[text()='Product Upload']"))
upload.click();

答案 1 :(得分:0)

Shivam Mishra回答了我的问题。

解决方案在iframe中: 解决方法:

driverFireFox.switchTo().frame("iframe6");

非常感谢Shivam!