gpt4 book ai didi

java - 无法获取 getText()

转载 作者:行者123 更新时间:2023-11-30 06:04:02 25 4
gpt4 key购买 nike

嗨,我正在学习 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com