Java 获取值时无法理解
我正在编写一个selenium代码来执行以下操作Java 获取值时无法理解,java,selenium,xpath,automated-tests,Java,Selenium,Xpath,Automated Tests,我正在编写一个selenium代码来执行以下操作 在文本框中输入值 选择下拉列表值 选择一个单选按钮 按“开始”按钮 当我这样做时,我会得到一个结果列表,我想得到第一个结果块的标题 下面是我的代码 import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remot
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class Test1 {
public static void main(String[] args) throws InterruptedException {
WebDriver driver;
System.setProperty("webdriver.gecko.driver", "C:\\Users\\home\\Downloads\\geckodriver.exe");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new FirefoxDriver(capabilities);
driver.get("https://www2.chubb.com/us-en/find-agent-page.aspx");
driver.findElement(By.xpath(".//*[@id='tbAddress']")).sendKeys("60089");
driver.findElement(By.xpath(".//*[@id='cphHeroContent_drpDistanceMiles']")).sendKeys("2");
driver.findElement(By.xpath(".//*[@id='cphHeroContent_rdType_0']")).click();
driver.findElement(By.xpath(".//*[@id='cphHeroContent_btnSearch']")).click();
String title = driver.getTitle().toString();
System.out.println(title);
Thread.sleep(10000L);
String getHeadingTitle = driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).toString();
System.out.println(getHeadingTitle);
}
}
在我的代码中,我能够完成步骤1、2、3,并且能够在控制台中获取标题名称
在尝试获取标题文本时,它给了我以下异常
JavaScript错误:,
第2行:SyntaxError:预期表达式,得到“您不应该使用
toString()
方法-使用getText()
:
driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).getText();