Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
在Selenium和Java中为find设置超时_Selenium_Xpath - Fatal编程技术网

在Selenium和Java中为find设置超时

在Selenium和Java中为find设置超时,selenium,xpath,Selenium,Xpath,我有这样的HTML <tr> <td> <label>...</label> </td> <td> <input>...</input> </td> <td> </td> </tr> <tr> <td> </td> <td> </td>

我有这样的HTML

<tr>
  <td>
    <label>...</label>
  </td>
  <td>
    <input>...</input>
  </td>
  <td>
  </td>
</tr>
<tr>
  <td>
  </td>
  <td>
  </td>
  <td>
    <a>  ...</a>
  </td>
</tr>
我浏览每一行,发现tds如下

 row.findElements(By.xpath("td"));
这没问题。然后我通过tds查找元素:

 td.findElements(By.xpath("*")) 
(请注意这是可以的,因为它不是//*) 因此,如果td有子元素,它会立即返回。但如果不是,它会一直等待直到超时

我做了一个测试

 driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
甚至是

 driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);
两者都不起作用,正如我所期望的那样,这两种方法只适用于

 driver.findelements(By.xpath)).
那么,有没有办法为元素查找设置此超时?这些tds没有ID 或者别的什么,或者我可以做一个

 driver.findElements(By.xpath(xpath with id + "//*")); 
超时可能会起作用

如果一个元素没有id、名称或诸如此类的东西,那么它有没有办法获得一个特定的xpath


所以有两个问题:1:如何设置元素超时,2:如果您有一个元素,如何为它获取xpath我看不出超时的原因。只需遍历所有行并获取数据。这是我写的脏代码

    String value;
    int rows = driver.findElements(By.xpath("//table//tr")).size();
    int columns = driver.findElements(By.xpath("//table//tr[1]/td")).size();
    for(int row = 1; row <rows+1; ++row) {
        for (int col =1; col<columns+1; col++) {
            //now check if there are any children with input hidden
            if (driver.findElements(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input[@type='hidden']")).size()>0) {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input[@type='hidden']")).getAttribute("value");
            }
            //now check if there are any children with input without hidden
            else if ((driver.findElements(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input")).size()>0)) {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input")).getAttribute("value"); 

            }
            //just get text as there are no children input
            else {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]")).getText();
            }
            System.out.println("Row:" + row + " Col:" + col + " value:" + value);
            }
    }
字符串值;
int rows=driver.findElements(By.xpath(“//table//tr”)).size();
int columns=driver.findElements(By.xpath(//table//tr[1]/td”).size();
对于(int行=1;行0)){
value=driver.findElement(By.xpath(“//table//tr[“+row+”]/td[“+col+”]/input”).getAttribute(“value”);
}
//只需获取文本,因为没有子输入
否则{
value=driver.findElement(By.xpath(“//table//tr[“+row+]”]/td[“+col+]”);
}
System.out.println(“行:“+Row+”列:“+Col+”值:“+value”);
}
}
不需要超时(除非表动态加载数据),请用脏代码查找我的答案。
    String value;
    int rows = driver.findElements(By.xpath("//table//tr")).size();
    int columns = driver.findElements(By.xpath("//table//tr[1]/td")).size();
    for(int row = 1; row <rows+1; ++row) {
        for (int col =1; col<columns+1; col++) {
            //now check if there are any children with input hidden
            if (driver.findElements(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input[@type='hidden']")).size()>0) {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input[@type='hidden']")).getAttribute("value");
            }
            //now check if there are any children with input without hidden
            else if ((driver.findElements(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input")).size()>0)) {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]/input")).getAttribute("value"); 

            }
            //just get text as there are no children input
            else {
                value = driver.findElement(By.xpath("//table//tr["+ row +"]/td["+ col +"]")).getText();
            }
            System.out.println("Row:" + row + " Col:" + col + " value:" + value);
            }
    }