问题
I'm writing a selenium code to do the below thing.
- Enter value in text box.
- Select the dropdown value.
- Select a radio button.
- Hit the go button.
When I do this, I will get a list of results and I want to get the heading of the first result block.
Below is my code.
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);
}
}
In my code, I'm able to get step1, 2, 3 done and I able to get the title name in my console.
It is giving me the below exception when trying to get the heading text.
JavaScript error: https://www2.chubb.com/us-en/find-agent-page.aspx, line 2: SyntaxError: expected expression, got '<'
JavaScript warning: https://www2.chubb.com/_Global-Assets/js/jquery-webdriver.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead [[FirefoxDriver: firefox on XP (320d5e47-8575-4566-9622-d8275cf72ded)] -> xpath: .//*[@id='chubbAgentData']/li/h2]
Please let me know where am I going wrong and how can I fix this.
回答1:
You should not be using toString() method - use getText():
driver.findElement(By.xpath(".//*[@id='chubbAgentData']/li/h2")).getText();
来源:https://stackoverflow.com/questions/41256484/unable-to-understand-on-getting-the-value