Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/311.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
Java Selenium不实现单击_Java_Selenium_Selenium Webdriver - Fatal编程技术网

Java Selenium不实现单击

Java Selenium不实现单击,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,我试图在表中的元素上实现单击。目前,我正在表格中搜索特定的字符串。如果字符串匹配,我将元素返回到其调用方法,并尝试实现click,但调用方法从未单击该元素 任何帮助都将不胜感激 检查表法 public static WebElement chk_TableContentsByXpath(String searchString, String elements){ WebElement element = null; try{ // Grab the

我试图在表中的元素上实现单击。目前,我正在表格中搜索特定的字符串。如果字符串匹配,我将元素返回到其调用方法,并尝试实现click,但调用方法从未单击该元素

任何帮助都将不胜感激

检查表法

public static WebElement chk_TableContentsByXpath(String searchString, String elements){
    WebElement element = null;
        try{
            // Grab the table
            WebElement table = driver.findElement(By.xpath(elements));

            // Now get all the TR elements from the table
            List<WebElement> allRows = table.findElements(By.tagName("tr"));
            // And iterate over them, getting the cells
            for (WebElement row : allRows) {
                List<WebElement> cells = row.findElements(By.xpath("./*"));
             for (WebElement cell : cells) {
                // System.out.println(cell.getText());
                    if(cell.getText().equals(searchString)){
                        element = cell;
                        return element;
                    }   
             }
            }
     }catch (Exception e){
                Log.error("Class Utils | Method GetTableContents | Exception occured while search table : "+e.getMessage());
                throw (e);
            }
    return element;
        }

如果要查找特定字符串,可以使用xpath:

WebElement table = driver.findElement(By.xpath(elements));

table.findElement(By.xpath("//tr[text() = '" + searchString + "']")).click();

您可以尝试以下任一过程:

1-尝试替换代码:

List cells=row.findElements(By.xpath(“./*”)

List cells=row.findElements(By.xpath(“//td”)

2-此外,您还可以使用下面的代码从方法chk_TableContentsByXpath直接返回元素:

WebElement table = driver.findElement(By.xpath(elements));

element = table.findElement(By.xpath("//td[contains(text(),searchString)]"));
WebElement table = driver.findElement(By.xpath(elements));

element = table.findElement(By.xpath("//td[contains(text(),searchString)]"));