Java 无法获取getText()

Java 无法获取getText(),java,eclipse,selenium,selenium-webdriver,Java,Eclipse,Selenium,Selenium Webdriver,嗨,我在学硒。我试图从FreeCrm.com的索引页获取用户名的geText(),我的xpath在firebug中是正确的。但是得到NoSuchElementExceptopn Url: 我的代码: @Test(description="verify the account holder name displaying correctly or not") public void AccHolderName() { driver=new Firef

嗨,我在学硒。我试图从FreeCrm.com的索引页获取用户名的geText(),我的xpath在firebug中是正确的。但是得到
NoSuchElementExceptopn

Url我的代码

    @Test(description="verify the account holder name displaying correctly or not")
        public void AccHolderName() {
            driver=new FirefoxDriver();
            driver.get("https://www.freecrm.com/index.html");
            driver.findElement(By.name("username")).sendKeys("xxxxx");
            driver.findElement(By.name("password")).sendKeys("xxxx");
            driver.findElement(By.xpath("//input[@class='btn btn-small']")).click();
            String AccName=driver.findElement(By.xpath("//td[@class='headertable']//table//tbody//td[@class='headertext' and @align='left']")).getText().trim();
            System.out.println(AccName);
错误:无法定位元素:

{"method":"xpath","selector":"//td[@class='headertable']//table//tbody//td[@class='headertext' and @align='left']"}

请帮助我编写代码

由于您的元素位于框架中,您必须首先切换到其内容:

new WebDriverWait(driver, 10).wait(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainpanel")));
然后,您必须等待元素在页面上可见:

// wait at least 10 seconds until element will be visible on tha page
new WebDriverWait(driver, 10).wait(ExpectedConditions.visibilityOf(driver.findElement(By.xpath("//td[@class='headertable']//table//tbody//td[@class='headertext' and @align='left']"))));
String AccName = driver.findElement(By.xpath("//td[@class='headertable']//table//tbody//td[@class='headertext' and @align='left']")).getText().trim();
System.out.println(AccName);

//switch back to default content
driver.switchTo().defaultContent();
注意:您必须添加一些导入:

import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.By;

不要共享凭据。发布相关的html。在呈现基本页面后,您希望从DOM中读取的项目可能不会呈现,我的意思是,它可能会通过AJAX呈现,因此您需要等待元素在页面上可见。非常感谢:)