我如何使用报价"或者'字符串中的字符?

时间:2014-08-04 13:23:56

标签: java string xpath selenium-webdriver

正在使用JAVA处理selenium webdriver!

我正在尝试使用变量exp

动态传递字符串
String exp=",,,4'-TETRA; P-CHLORIDE";

d.findElement(By.xpath("a[contains(text(),\""+exp+"\")]//ancestor::table//parent::div")).sendKeys(Keys.ARROW_DOWN);

但它给了我一个错误:

Unable to locate element: {"method":"xpath","selector":

"a[contains(text(),\",,,4'-TETRA; P-CHLORIDE\")]//ancestor::table//parent::div"

}

1 个答案:

答案 0 :(得分:2)

使用'"+exp+"'代替\""+exp+"\"

  d.findElement(By.xpath("a[contains(text(),'"+exp+"')]//ancestor::table//parent::div")).sendKeys(Keys.ARROW_DOWN);

<强>更新

d.findElement(By.xpath('a[contains(text(),"'+exp+'")]//ancestor::table//parent::div')).sendKeys(Keys.ARROW_DOWN);