Selenium中缺少Get方法

问题描述 投票:-1回答:1

我可能正在寻找或做错了事情,但是当我尝试在Selenium中使用GetAttributeGetText方法时,它们不存在吗?

例如,有一个“忘记密码”链接,使用该ID很容易找到,但是上面有一些文字,上面有说明,如果用户忘记了密码,请用户选择按钮。我希望使用GetText,但是那不是我的下拉选项;我只得到:GetHashCodeGetTypeGetScreenShot。它是在我没有的Nuget包中还是应该作为Selenium的标准配置?

public IWebElement ForgotPasswordText { get { return _driver.gettext(("Please Click Below To Reset Your Password")); } }

Assert.IsTrue(HomePage.ForgotPasswordText.Displayed);

我曾经在项目中使用过此功能,但现在并没有打扰,也无法确定差异以查看ive在哪里出错。

GetText不存在,但通常会使用该选项自动填充。

c# selenium automation nuget gettext
1个回答
0
投票

据我所知,您正在尝试使用GetAttributeGetText定位WebElement。这些方法只能用于已经定位的WebElements对象。要找到您的ForgotPasswordText元素,可以使用以下方法:

public IWebElement ForgotPasswordText { get { return _driver.FindElement(By.XPath("//*[text()='Please Click Below To Reset Your Password']")); } }

这将为您提供一个带有文本“请单击下面以重置密码”的WebElement。

现在,如果您想实际获取WebElement的文本或属性,可以按照以下步骤进行:

var webElement = Driver.FindElement(By.XPath("//div[@class='someClass']"));

var text = webElement.Text;
var classAttribute = webElement.GetAttribute("class");
var valueAttribute = webElement.GetAttribute("value");
© www.soinside.com 2019 - 2024. All rights reserved.