在表中搜索值,然后单击同一表的另一列中的相应链接

时间:2016-05-05 07:37:26

标签: selenium-webdriver

enter image description here我想在表中搜索特定值并单击同一行中另一列中的相应链接。以下是我写的代码。我现在获得了单元格内容的表值需要搜索并单击链接

   package checking;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
public class TableCorrespondingValueClick {
    public static void main(String[] args) {
        RemoteWebDriver driver;
        //driver = new FirefoxDriver();
        System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("http://toolsqa.com/automation-practice-table");
        String Searchvalue = "Taiwan";
        List<WebElement> heading = driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr/th"));
        List<WebElement> row = driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr"));
        List<WebElement> cell =  driver.findElements(By.xpath(".//*[@id='content']/table/tbody/tr/td"));
        int rowcount = row.size();
        int cellcount = cell.size();
        System.out.println("No.of Rows in the Table : "+rowcount);
        System.out.println("No.of Cells in the Table : "+cellcount);
        for (WebElement c : cell) {
            String cellvalue = c.getText();
            System.out.println("Cell Values : "+cellvalue);
            if (cellvalue.equalsIgnoreCase(Searchvalue)) {
                System.out.println("Found");
            }
        }
        driver.quit();
    }
}

Can anyone suggest me the solution for this scenario

enter image description here

HTML源

<table class="tsc_table_s13" style="width: 100%;" summary="Sample Table" border="1">
<caption><strong>Sample Table</strong></caption>
<thead>
<tr>
<th style="text-align: justify;" scope="col">Structure</th>
<th scope="col">Country</th>
<th scope="col">City</th>
<th scope="col">Height</th>
<th scope="col">Built</th>
<th scope="col">Rank</th>
<th scope="col">&#8230;</th>
</tr>
</thead>
<tfoot>
<tr>
<th scope="row">Total</th>
<td colspan="7">4 buildings</td>
</tr>
</tfoot>
<tbody>
<tr>
<th scope="row">Burj Khalifa</th>
<td>UAE</td>
<td>Dubai</td>
<td>829m</td>
<td>2010</td>
<td>1</td>
<td><a href="#">details</a></td>
</tr>
<tr class="odd">
<th scope="row">Clock Tower Hotel</th>
<td>Saudi Arabia</td>
<td>Mecca</td>
<td>601m</td>
<td>2012</td>
<td>2</td>
<td><a href="#">details</a></td>
</tr>
<tr>
<th scope="row">Taipei 101</th>
<td>Taiwan</td>
<td>Taipei</td>
<td>509m</td>
<td>2004</td>
<td>3</td>
<td><a href="#">details</a></td>
</tr>
<tr class="odd">
<th scope="row">Financial Center</th>
<td>China</td>
<td>Shanghai</td>
<td>492m</td>
<td>2008</td>
<td>4</td>
<td><a href="#">details</a></td>
</tr>
</tbody>
</table>

2 个答案:

答案 0 :(得分:0)

请按以下方式进行

public class workingwithTable {

public static void main(String[] args) {

    WebDriver driver = new FirefoxDriver();
    driver.get("file:///C:/Users/rajnish/Desktop/Table.html");
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    List<WebElement> allvalue = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr"));
    // for printing everything in the table
    for(int i =0;i<allvalue.size();i++){
        System.out.println("Value are : " + allvalue.get(i).getText());
    }

    // for printing a particular column rows
    List<WebElement> allCountrymvalue = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr/td[1]"));

    for(int i =0;i<allCountrymvalue.size();i++){
        System.out.println("Value are : " + allCountrymvalue.get(i).getText());
    }

    // for printing all links do it like belwo
    List<WebElement> alllinks = driver.findElements(By.xpath("//*[@class='tsc_table_s13']/tbody/tr/td[6]/a"));

    for(int i =0;i<alllinks.size();i++){
        System.out.println("Link is  : " + alllinks.get(i).getText());
    }

    // Now please note that inside the web-table all column and rows are fixed
    // i.e size of each row for each column will be fixed 
    // i.e if size for Country is = 4 then ,size for City ,height ,build,rank 
    // and for link will be same i.e =4

    // hence on that basic we can do it like below 
    String MyCountryVal = "Taiwan";
    for(int i =0;i<allCountrymvalue.size();i++){
        System.out.println("Value are : " + allCountrymvalue.get(i).getText() + "== Corresponding link is : " + alllinks.get(i).getText());
        // now on the basis of column value click the corresponding link
        if(allCountrymvalue.get(i).getText().equals(MyCountryVal)){
            alllinks.get(i).click();
            break;
        }
    }
}
}

答案 1 :(得分:0)

尝试如下:

driver.manage().window().maximize();
driver.get("http://toolsqa.com/automation-practice-table");
Thread.sleep(4000);
String Searchvalue = "Taiwan";

//get name and details link element.
List<WebElement> countryname = driver.findElements(By.cssSelector("tbody>tr>th+td"));
List<WebElement> link = driver.findElements(By.cssSelector("tbody>tr>td>a"));

System.out.println("No.of Rows in the Table : "+countryname.size());
System.out.println("No.of link in the Table : "+link.size());

for (int i =0; i<countryname.size();i++) {

    String country = countryname.get(i).getText();
    System.out.println("country Values : "+country);

    if (country.equalsIgnoreCase(Searchvalue)) {
        System.out.println("Found");
        link.get(i).click();
        break;
    }
}
相关问题