简体   繁体   English

硒C#如何在IWEbElement中获取文本

[英]Selenium C# how to get text in IWEbElement

I am trying to fill in a text-field on a website for which the id changes every time the website is opened. 我正在尝试在网站上填写一个文本字段,每次打开该网站时ID都会更改。 To circumvent this, I look up the webelement by it's class name and this seems to work well as I am able to click on it through 为了避免这种情况,我通过它的类名查找了webelement,并且看起来很不错,因为我可以单击它

currentWebElement.Click();

however, when I try to fill in the edit text box through 但是,当我尝试通过

currentWebElement.SendKeys("51");

nothing happens (even more so, focus is lost) 什么也没发生(更是如此,失去了焦点)

I have searched a lot to see what the problem is but have not been able to find a solution so I was wondering if someone here can point me in the right direction. 我进行了很多搜索以查找问题所在,但未能找到解决方案,因此我想知道这里是否有人可以为我指明正确的方向。 What I have tried so far: 到目前为止我尝试过的是:

1) I was working in chrome but since this seems to be a common problem with sendKeys I have swithed to ie, alas the problem persisted. 1)我在chrome中工作,但是由于这似乎是sendKeys的常见问题,所以我仍然坚持该问题。 I have tried migrating to firefox but did not succeed as I cannot seem to locate the binary path to firefox.exe but this should be addressed in a different question. 我曾尝试迁移到firefox,但未成功,因为我似乎无法找到firefox.exe的二进制路径,但这应该在另一个问题中解决。 So the problem occurs both in chrome and IE. 因此,Chrome和IE均会出现此问题。

2) Using javascript to send the command as I have found this suggestion in other questions that were answered on this website: 2)使用javascript发送命令,因为我在此网站上回答的其他问题中发现了此建议:

driver.ExecuteScript("arguments[0].value = '51'", currentWebElement);

directly but also implementing it as a function did not work: 直接但也无法将其实现为功能:

private static object setValue(this IWebDriver driver, IWebElement element, String value) {
        return ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value = arguments[1]", element, value);
    }

// and then calling it in my code:

 setValue(driver, currentWebElement, "51");

all implementations did not give me errors nor warnings (I managed to get rid of those during the process), but none of them give the desired result ie the box keeps remaining empty which is really starting to bug me! 所有的实现都没有给我错误或警告(我设法消除了过程中的那些错误),但是它们都没有给出期望的结果,即该框保持空白,这真的开始惹恼我!

Try the below code. 试试下面的代码。 I have used XPATH to sendKeys in the field( FYI the PRMT_TB part of the id doesn't change, even if the rest part does each time, hence the xpath ): 我已经使用XPATH在字段中发送sendKeys( 仅供参考,id的PRMT_TB部分不会更改,即使其余部分每次都更改,因此xpath ):

var currentWebElement = driver.FindElement(By.XPath("//input[starts-with(@id,'PRMT_TB')]"));
currentWebElement.SendKeys("51");

OR 要么

In case the above doesn't work, please try the below code too 如果以上方法无效,请尝试以下代码

var currentWebElement = driver.FindElement(By.XPath("//td[@class='clsTextWidgetParseError']/input"));
currentWebElement.SendKeys("51");
By byCss = By.CssSelector(".clsTextWidgetParseError>input");
var element = Driver.FindElement(byCss );
element.Clear();
element.SendKeys(value);

I am assuming you are using wrong selector. 我假设您使用的是错误的选择器。 If this throws any exception provide me the stack trace please 如果抛出任何异常,请向我提供堆栈跟踪

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM