Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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 在标题数据和第一列数据匹配的位置选择一个文本框_Java_Selenium_Webdriver - Fatal编程技术网

Java 在标题数据和第一列数据匹配的位置选择一个文本框

Java 在标题数据和第一列数据匹配的位置选择一个文本框,java,selenium,webdriver,Java,Selenium,Webdriver,这就是我需要处理的页面的外观 场景:-我需要遍历该表,当标题DataH001等和第一列dataABC等与用户输入的数据匹配时,我需要单击对应的文本框 我已经编写了以下指定代码,但它不起作用:- public static void getMarksBox(WebDriver driver, String user, String taskCode) { UserData userNm = TestData.findUserById(user); String userName =

这就是我需要处理的页面的外观

场景:-我需要遍历该表,当标题DataH001等和第一列dataABC等与用户输入的数据匹配时,我需要单击对应的文本框

我已经编写了以下指定代码,但它不起作用:-

public static void getMarksBox(WebDriver driver, String user, String taskCode) {
    UserData userNm = TestData.findUserById(user);
    String userName = userNm.getName();
    WebElement table = WaitUtils.waitForElement(driver, By.cssSelector("table.eds-o-table.cvr-c-table--marksbook"));
    List<WebElement> tableCols = table.findElements(By.cssSelector("td.eds-o-table__cell"));
    int columnIndex = -1;
    for(int i=1; i<tableCols.size();i++)
    {
        if(userName.equals(tableCols.get(i).findElement(By.cssSelector(".v-label-cvr-c-data-nav-link")).getText()))
        {
            columnIndex = i;
            break;
        }
    }
    List<WebElement> tableRows = table.findElements(By.cssSelector("tr.eds-o-table__row"));
    List<WebElement> tableHeaders = tableRows.get(1).findElements(By.cssSelector(".v-label-cvr-u-margin-right--sm"));
    WebElement textBox = table.findElement(By.cssSelector(".v-textfield"));
    for(WebElement header :tableHeaders)
    {
        if(taskCode.equals(header.getText()))
        {
            textBox = tableRows.get(columnIndex);
            textBox.click();
            WaitUtils.sleepInSeconds(5);
            break;
        }
    }
}

使用下面的xpath直接访问inputbox,而不是执行上面方法中编写的循环

//td[position()=count(//th[contains(.,'First Name')]/preceding-sibling::th)+1 and normalize-space(.)='ABC']/ancestor::tr//td[position()=count(//th[contains(.,'BH001')]/preceding-sibling::th)+1]//input[contains(@class,'v-textfield-eds-c-input')]
以下是一般符号:


正如@supputuri所建议的,您可以通过XPath/Css选择器直接找到匹配的行或单元格,以避免复杂的循环,从而减少执行时间

public static void getMarksBox(WebDriver driver, String user, String taskCode) {

  UserData userNm = TestData.findUserById(user);
  String userName = userNm.getName();

  WebElement table = WaitUtils.waitForElement(driver, 
        By.cssSelector("table.eds-o-table.cvr-c-table--marksbook"));

  WebElement matchedRow = table.findElement(By.xpath(
        String.format("./tobdy/tr[td[1][normalize-space(.)='%s']]", userName)))

  WebElement matchedTextBox = matchedRow.findElement(
        By.cssSelector("./td:nth-child(2) input.v-textfield-eds-c-input"))

  matchedTextBox.click()

  // or you can directly find the matchedTextBox in one findElement
  String xpath = String.format(
       "./tobdy/tr[td[1][normalize-space(.)='%s']]" + 
       "/td[2]//input[contains(@class,'v-textfield-eds-c-input')]", userName)
  WebElement matchedTextBox = table.findElement(By.xpath(xpath))
  matchedTextBox.click()   
}

请将HTML代码添加为文本而不是图像。并分享您面临的问题/错误,而不是代码不起作用。一、 就我个人而言,我不知道你在这里想要什么。什么不起作用?您是否有任何错误?我们无法查看您的所有HTML,但我看不到任何具有v-label-cvr-u-margin-right-sm类的元素,除非我缺少某些内容。问题是:我需要选择与匹配项(如BH0001&DEF)对应的文本框。但我无法选择该文本框。@BhoomikaDatta请检查我在下面的答案中提供的xpath。如果不行,请告诉我。
public static void getMarksBox(WebDriver driver, String user, String taskCode) {

  UserData userNm = TestData.findUserById(user);
  String userName = userNm.getName();

  WebElement table = WaitUtils.waitForElement(driver, 
        By.cssSelector("table.eds-o-table.cvr-c-table--marksbook"));

  WebElement matchedRow = table.findElement(By.xpath(
        String.format("./tobdy/tr[td[1][normalize-space(.)='%s']]", userName)))

  WebElement matchedTextBox = matchedRow.findElement(
        By.cssSelector("./td:nth-child(2) input.v-textfield-eds-c-input"))

  matchedTextBox.click()

  // or you can directly find the matchedTextBox in one findElement
  String xpath = String.format(
       "./tobdy/tr[td[1][normalize-space(.)='%s']]" + 
       "/td[2]//input[contains(@class,'v-textfield-eds-c-input')]", userName)
  WebElement matchedTextBox = table.findElement(By.xpath(xpath))
  matchedTextBox.click()   
}