【发布时间】:2019-03-03 11:37:23
【问题描述】:
我想使用一个函数在屏幕上显示下拉列表的当前值,因为 Selenium 代码并不漂亮。
以下内容在我的主脚本中完美运行:
new Select(driver.findElement(By.id("secId:mainBody:vboxlist:s_m9_aa2::content"))).selectByVisibleText(contract.getCreditype());
但是使用该功能,它不起作用:
writeScreen("Description : ", "secId:mainBody:vboxlist:s_m9_aa2::content");
public void writeScreen(String description, String identifiant) {
String xpath="//*[@id=\\\""+identifiant+"\\\"]";
Select select = new Select(driver.findElement(By.xpath(xpath)));
WebElement option = select.getFirstSelectedOption();
String defaultItem = option.getText();
System.out.println(description+defaultItem);
我收到以下错误:
org.openqa.selenium.InvalidSelectorException:无效选择器:无法 使用 xpath 表达式定位元素 //[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"] 因为 以下错误:语法错误:无法执行“评估” “文档”:字符串 '//[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"]' 不是 有效的 XPath 表达式。
当我检查 XPATH 时,它与我的第一个工作示例完全相同。
【问题讨论】: