java - 使用 selenium webdriver 从文件中获取值并从下拉列表中选择相同的值

标签 java selenium selenium-webdriver io dropdown

我正面临这个问题,我正在尝试从 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/

相关文章:

java - 通用 DTO 转换器模式

java - Java 的 AU 音频文件

java - 显示在 org. springframework. validation.jsp 对象上设置的错误

python - 无法在容器内启动 selenium 测试,WebDriverException :Chrome failed to start: exited abnormally

java - 使用 Selenium webdriver 从文件系统拖放文件

selenium-webdriver - Canopy - 使用选择列表

javascript - 使用 Python 使用 Selenium 将 CSS/Javascript 动画渲染为一系列图像文件

java - 有什么方法可以在 Java 中使用 @BeforStep 和 @AfterStep 注释?

python - 属性错误问题 : module 'selenium.webdriver' has no attribute 'Chrome'

java - 如何使用 Selenium Webdriver 以移动模式打开 Chrome 浏览器以在 JMeter 中进行负载测试?