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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.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中使用Xpath获取第个索引号_Java_Selenium_Selenium Webdriver_Xpath_Html Table - Fatal编程技术网

Java 在selenium中使用Xpath获取第个索引号

Java 在selenium中使用Xpath获取第个索引号,java,selenium,selenium-webdriver,xpath,html-table,Java,Selenium,Selenium Webdriver,Xpath,Html Table,嗨,我只想使用唯一的Xpath获取第TH列的索引号。Like循环将运行并检查Xpath第列中的哪个匹配并返回索引号。有没有什么方法可以在selenium中实现这一点?到目前为止,我能够获得标记索引并运行循环,但现在我已经用xpath检查了每个TH上的,而不是它是否匹配,并给我索引号。请让我知道,我是否能够通过此逻辑或任何其他Xpath技术实现这一点 Table = driver.findElement(By.xpath("//table/thead[@class='ui-datatable-th

嗨,我只想使用唯一的Xpath获取第TH列的索引号。Like循环将运行并检查Xpath第
列中的哪个匹配并返回索引号。有没有什么方法可以在selenium中实现这一点?到目前为止,我能够获得标记索引并运行循环,但现在我已经用xpath检查了每个TH上的,而不是它是否匹配,并给我索引号。请让我知道,我是否能够通过此逻辑或任何其他
Xpath
技术实现这一点

Table = driver.findElement(By.xpath("//table/thead[@class='ui-datatable-thead']"))

List<WebElement> rows_head = Table.findElements(By.tagName('th'))

int head_size= rows_head.size()

System.out.println(head_size);

for (int c = 1; c <= head_size; c++) {
    driver.findElement(By.xpath(
       "//th[4][@class='ui-state-default ui-unselectable-text ui-sortable-column']")
    )
    // Here is something I want loop will check the each TH with above given
    // Xapth and return the TH index in the table on match TH xpath index.
}
Table=driver.findElement(By.xpath(“//Table/thead[@class='ui-datatable-thead']))
列表行\u head=Table.findElements(按.tagName('th'))
int head\u size=行\u head.size()
系统输出打印LN(头部尺寸);

for(int c=1;cSelenium没有提供返回找到的元素的xpath的API。因此,您无法通过比较xpath来归档您的目标

但是Selenium提供API来获取找到的元素的属性值,您可以与它进行比较,以确定它是否是您想要的元素

WebElement thead = driver.findElement(By.xpath("//table/thead[@class='ui-datatable-thead']"));

List<WebElement> heads = thead.findElements(By.tagName('th'));

int head_size= heads.size()

System.out.println(head_size);

String expectedClass = "ui-state-default ui-unselectable-text ui-sortable-column"

int i = 1;
for (; i <= head_size; i++) {

    if(heads[i].getAttribute("class").equal(expectedClass))
      // check attribute class value is as expect
      // you can change to other attribute or check more than one attribute 
      break;
    )
}

System.out.println("Matched th index: " + i);
WebElement thead=driver.findElement(By.xpath(“//table/thead[@class='ui-datatable-thead']);
列表头=ad.findElements(按.tagName('th'));
int head_size=heads.size()
系统输出打印LN(头部尺寸);
String expectedClass=“ui状态默认ui不可选择文本ui可排序列”
int i=1;

对于(;我感谢您是的,我已经通过xapth实现了这一点,然后获取文本内容:)