如何使用Selenium和Java单击WebTable的任何页面上存在的WebElement

时间:2018-06-28 06:32:32

标签: java selenium

大家好,我试图单击Webtable的每个页面(即WebTable的分页)上都存在的WebElement(CheckBox),但是我想单击通过Xpath传递的Perticular Checkbox。 / strong>

下面是我尝试过的代码

Private void Button_Click(object sender, RoutedEventArgs e)
{
    read_gps_data();
}

但是我在这里面临的问题是HTML: <form id="vContactsForm" action="https://www.freecrm.com/system/index.cfm" method="post"> <table class="datacard" width="100%" cellspacing="3" cellpadding="2" border="0"> <tbody><tr><td class="datacardtitle" colspan="8" valign="top" align="left"> <table width="100%" cellspacing="0" cellpadding="0" border="0"> <tbody><tr><td class="datacardtitle" valign="top" align="left"> Contacts </td> <td class="datacardtitle" valign="top" align="right"> <input class="button" value="Export" onclick="exportResultSet('query_slct_contacts_full');" type="button"> | <input class="button" value="Shortlist All Results" onclick="flagResultSet('query_slct_contacts_full', 'CONTACT', this);" type="button"> | <input class="button" value="New Contact" onclick="go('https://www.freecrm.com/system/index.cfm?action=contact&amp;sub=load&amp;client_id=&amp;edit=1');" type="button"> </td></tr></tbody></table> </td></tr> <tr><td colspan="7" valign="top" align="left"> <div class="pagination">Pages : <strong>1</strong> | <a href="https://www.freecrm.com/system/index.cfm? sr=21&amp;action=contact&amp;sub=start&amp;fl=&amp;">2</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=41&amp;action=contact&amp;sub=start&amp;fl=&amp;">3</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=61&amp;action=contact&amp;sub=start&amp;fl=&amp;">4</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=81&amp;action=contact&amp;sub=start&amp;fl=&amp;">5</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=101&amp;action=contact&amp;sub=start&amp;fl=&amp;">6</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=121&amp;action=contact&amp;sub=start&amp;fl=&amp;">7</a> | <a href="https://www.freecrm.com/system/index.cfm? sr=141&amp;action=contact&amp;sub=start&amp;fl=&amp;">8</a> | </div> </td> <td valign="top" align="right"> Java Code: public class ClickOnAnyPageLink { public static void main(String[] args) throws InterruptedException { System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir")+"//All_Drivers//chromedriver.exe"); WebDriver driver = new ChromeDriver(); driver.get("https://freecrm.com"); driver.manage().window().maximize(); driver.manage().deleteAllCookies(); driver.findElement(By.name("username")).sendKeys("*****"); driver.findElement(By.name("password")).sendKeys("*****"); driver.findElement(By.xpath("//input[@type='submit' and @value='Login']")).click(); driver.switchTo().frame("mainpanel"); driver.findElement(By.xpath("//a[contains(text(),'Contacts')]")).click(); Thread.sleep(5000); List<WebElement> all_pages = driver.findElements(By.xpath("//form[@id='vContactsForm']/descendant::div[1]/a")); System.out.println(all_pages.size());//7 Iterator<WebElement> pageiterator = all_pages.iterator(); while(pageiterator.hasNext()) { WebElement elem = pageiterator.next(); elem.click();// on second page Thread.sleep(3000); WebElement elem1 = driver.findElement(By.xpath("//a[contains(text(),'nnn jhjhjk')]/parent::*/preceding-sibling::*/child::*")); if(elem1.isDisplayed()) { elem1.click(); break; } } driver.close(); } } 。因为页面仅被单击一次,所以它不会为WebTable的每个页面进行迭代并检查该Perticular复选框的存在。

此xpath发生异常: Element not found Exception

我不明白我在这里想念的是什么,希望我很清楚。任何帮助将不胜感激。谢谢

2 个答案:

答案 0 :(得分:0)

能否请您尝试将if循环更改为以下内容,并让我知道它是否对您有用-

List<WebElement> elem1 = driver.findElements(By.xpath("//a[contains(text(),'nnn jhjhjk')]/parent::*/preceding-sibling::*/child::*"));

        if(elem1.size()>0)
        {
            elem1.click();  
            break;
        }

这是假设您仅获得一个元素。请让我知道它是否有效。

答案 1 :(得分:0)

List<WebElement> all_pages = 
driver.findElements(By.xpath("//form[@id='vContactsForm']/descendant::div[1]/a"));

    int sizeosallpages = all_pages.size();

    for(int i=2;i<=sizeosallpages;i++)
    {

        driver.findElement(By.xpath("//form[@id='vContactsForm']/descendant::div[1]/a["+i+"]")).click();
        Thread.sleep(3000);



        if(driver.getPageSource().contains("nnn jhjhjk"))
        {

            driver.findElement(By.xpath("//a[contains(text(),'nnn jhjhjk')]/parent::*/preceding-sibling::*/child::*")).click();
            Thread.sleep(3000);
            break;
        }


    }