上面的代码给出了错误 Invalid character constant in selenium with java

问题描述

String wb = driver.findElement(By.xpath("//div[@class='gb_h gb_i']/a")).getAttribute('href');

以上代码给出错误 Invalid character constant
我想使用 java

从 html 代码获取 href

解决方法

您应该将 getAttribute('href') 更改为 getAttribute("href") 所以它应该是
String wb = driver.findElement(By.xpath("//div[@class='gb_h gb_i']/a")).getAttribute("href");
getAttribute() 方法接收 String
在 Java 中,String" " 定义,而 Character' '

定义