Java 将字符串转换为WebElement

Java 将字符串转换为WebElement,java,webdriver,Java,Webdriver,是否可以将WebElement的字符串表示形式转换为WebElement 如果这是一个类变量: @FindBy(css = "select[id*='budTbl:0:j_id544']") WebElement roleList1; 还有一个字符串设置为roleList1 字符串是否可以转换为WebElementroleList1,以便在以下语句中使用 new Select(roleList1).getFirstSelectedOption().getText() 不,不能将字符串转换为

是否可以将
WebElement
的字符串表示形式转换为
WebElement

如果这是一个类变量:

@FindBy(css = "select[id*='budTbl:0:j_id544']") 
WebElement roleList1;
还有一个字符串设置为roleList1

字符串是否可以转换为
WebElement
roleList1,以便在以下语句中使用

new Select(roleList1).getFirstSelectedOption().getText()

不,不能将字符串转换为WebElement

相反,你可以这样做

if (myString.equals("roleList1"))
    new Select(roleList1).getFirstSelectedOption().getText();
没有。 字符串到Webelement的转换非常棘手。 它需要dom和所有的知识

但若您只想比较页面上是否存在相同的名称元素,请使用下面的

public boolean methodname(String element_string_name)
{ 
val = false;
if (element_string_name.equals("string_name")) {
                element_name.isDisplayed();
                val = true;
}
return val;
}

您不能直接将
字符串
转换为
WebElement
,但可以使用下面的字符串执行某些操作

string element = "("+MembersInfo_Content.ToString()+")[position()]["+i.ToString()+"]";
string memberInfo = driver.FindElement(By.XPath(element)).GetTextContent();

最简单的技巧之一是:

假设您需要在xpath中修改某些内容,在本例中,您可以简单地以如下所示的字符串形式声明该xpath,然后使用.replace方法修改该xpath:

 String xpath = "//span[contains(text(),'STR')]";
 xpath.replace("STR",str);
然后使用selenium调用这个XPath,比如

driver().findElement(By.xpath(xpath));