【问题标题】:Unable to take getText()无法获取 getText()
【发布时间】:2018-08-07 12:06:27
【问题描述】:

嗨,我正在学习硒。我正在尝试从 FreeCrm.com 的索引页面获取用户名的 geText(),我的 xpath 从萤火虫中是正确的。但得到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']"}

请帮我写代码

【问题讨论】:

  • 不要共享凭据。而是发布相关的html。
  • 您要从 DOM 中读取的项目可能不会在呈现基本页面后呈现,我的意思是它可能会通过 AJAX 呈现,因此您需要等到元素在页面

标签: java eclipse selenium selenium-webdriver


【解决方案1】:

由于你的元素在框架中,你必须先切换到它的内容:

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;

【讨论】:

  • 非常感谢你:)
猜你喜欢
  • 2013-07-02
  • 2019-10-05
  • 1970-01-01
  • 2013-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 1970-01-01
相关资源
最近更新 更多