我正面临这个问题,我正在尝试从 Excel 文件中读取并从屏幕上的下拉列表中选择相应的值。使用代码:
cell = sheet.getRow(i).getCell(4);
cell.setCellType(Cell.CELL_TYPE_STRING);
WebElement dr = driver.findElement(By.xpath("//*[@id =\"select2chosen-3\"]"));
dr.click();
driver.findElement(By.xpath("//li/div[contains(.,cell.getStringCellValue())]")).click();
从 eclipse 获取此错误:
org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //li/div[contains(.,cell.getStringCellValue())] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//li/div[contains(.,cell.getStringCellValue())]' is not a valid XPath expression.
最佳答案
尝试使用以下代码,
driver.findElement(By.xpath("//li/div[contains(.,'"+cell.getStringCellValue().trim()+"')]")).click();
关于java - 使用 selenium webdriver 从文件中获取值并从下拉列表中选择相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51626415/