java - 无法获取 getText()

标签 java eclipse selenium selenium-webdriver

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

网址:https://www.freecrm.com 我的代码:

    @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;

关于java - 无法获取 getText(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51726413/

相关文章:

java - 如何解决 "unreported exception AWTException ; must be caught or declared to be thrown"。机器人实例

java - 如何修改读取 vector 的正则表达式来读取矩阵?

java - 如何删除调试器中 toString() 输出的上限?

android - eclipse 找不到我的 sdk 文件夹

java - WebDriver等待,不同的等待条件

java - 在 Eclipse 中安装 Glassfish 服务器工具时出错

java - 字符类中的元字符 (`[]` )

java - 关闭 "error"关于在 Eclipse 中缺少无参数构造函数

java - 具有优先级的测试用例执行的 TestNG 顺序

java - 为什么 XPath last() 函数不能像我预期的那样工作?