gpt4 book ai didi

java - Selenium : XPATH invalid when stocking it in a String

转载 作者:行者123 更新时间:2023-12-02 10:46:22 25 4
gpt4 key购买 nike

我想使用一个函数在屏幕上显示下拉列表的当前值,因为 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: invalid selector: Unable to locate an element with the xpath expression //[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//[@id=\"secId:mainBody:vboxlist:s_m9_aa2::content\"]' is not a valid XPath expression.

当我检查 XPATH 时,它与我的第一个工作示例中的完全相同。

最佳答案

您的xpath错误,请更改String xpath="//*[@id=\\\""+identifiant+"\\\"]";如下:

String xpath="//*[@id='"+identifiant+"']";

关于java - Selenium : XPATH invalid when stocking it in a String,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52538948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com