简体   繁体   English

如何找到动态元素并通过Selenium和C#发送文本

[英]How to find a dynamic element and send text through Selenium and C#

EDIT: Not sure if it really helps to figure it out, but this is Sharepoint based. 编辑:不确定是否真的有帮助弄清楚,但这是基于Sharepoint的。

I have an element with special character which the Webdriver can't locate. 我有一个带有特殊字符的元素,Webdriver无法找到它。

var element = wait.Until(x => x.FindElement(By.Id("Tasrit_6aecdca9-e3b9-4141-ae36-d537784f9592_$TextField_inplacerte")));
element.SendKeys("foo");

I guess it is the $ that causes the problem. 我想是导致问题的$

On the contrary , I found it by using : 相反,我通过使用以下命令找到了它:

var element = wait.Until(x => x.FindElements(By.CssSelector("div[id*='Tasrit_6aecdca9-e3b9-4141-ae36-d537784f9592']")));
element[2].FindElement(By.TagName("p")).SendKeys("foo");

The test passes that way(seemingly), but the value isn't really being sent to the field. 测试以这种方式通过(貌似),但是该值并未真正发送到该字段。 Unfortunately, there is no input tag on the element's hierarchy, and when inserting the text manually , I can then see that the value was inserted to the <p> tag. 不幸的是,元素的层次结构上没有input标签,并且当手动插入文本时,我可以看到该值已插入到<p>标签中。 But , as shown, when using the <p> tag , it doesn't really help. 但是,如图所示,使用<p>标记时,它并没有真正的帮助。

The HTML: HTML:

 <div class="ms-rtestate-write ms-rteflags-0 ms-rtestate-field" id="Tasrit_6aecdca9-e3b9-4141-ae36-d537784f9592_$TextField_inplacerte" role="textbox" aria-haspopup="true" aria-labelledby="Tasrit_6aecdca9-e3b9-4141-ae36-d537784f9592_$TextField_inplacerte_label" style="min-height: 84px;" contenteditable="true" aria-autocomplete="both" aria-multiline="true" RteDirty="true"> <p> <span id="ms-rterangecursor-start" RteNodeId="1"></span> <span id="ms-rterangecursor-end"></span> ​</p> </div> 

Instead of implementing two FindElement* you can do it in single step as follows: 无需实现两个FindElement*而是可以按以下步骤进行操作:

  • CssSelector : CssSelector

     wait.Until(x => x.FindElement(By.CssSelector("div.ms-rtestate-write.ms-rteflags-0.ms-rtestate-field[id^='Tasrit_'][aria-labelledby$='_inplacerte_label']>p"))).SendKeys("foo"); 
  • XPath : XPath

     wait.Until(x => x.FindElement(By.XPath("//div[@class='ms-rtestate-write ms-rteflags-0 ms-rtestate-field' and starts-with(@id,'Tasrit_')][contains(@aria-labelledby,'_inplacerte_label')]/p"))).SendKeys("foo"); 

Update 更新

However the element looks dynamic to me so you need to induce WebDriverwait for the desired element to be clickable and you can use either of the following solutions: 但是,该元素对我来说似乎是动态的,因此您需要诱使WebDriverwait使所需的元素可单击,并且可以使用以下任一解决方案:

  • CssSelector : CssSelector

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("div.ms-rtestate-write.ms-rteflags-0.ms-rtestate-field[id^='Tasrit_'][aria-labelledby$='_inplacerte_label']>p"))).SendKeys("foo"); 
  • XPath : XPath

     new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[@class='ms-rtestate-write ms-rteflags-0 ms-rtestate-field' and starts-with(@id,'Tasrit_')][contains(@aria-labelledby,'_inplacerte_label')]/p"))).SendKeys("foo"); 

And I guess there is extra space in that element's id :) Try this: 而且我猜该元素的id中还有多余的空间 :)试试这个:

wait.Until(x => x.FindElement(By.Id("Tasrit_6aecdca9-e3b9-4141-ae36-d537784f9592_$TextField_inplacerte")));
element.Click();

Either that or the Id value of that element in the DOM is different , $TextField_inplacerte being a variable that parses to eg. 或DOM中该元素的ID值不同$TextField_inplacerte是一个解析为例如的变量。 spaceMonkey, undefined, 5 etc. Open up the dev tools, find the element, right-click, inspect and confirm the actual Id of the element in the DOM. spaceMonkey,undefined,5等。打开开发工具,找到元素,右键单击,检查并确认DOM中元素的实际ID。

You can use dev tools API to search for text in DOM of the currently opened page (with that element in it) if it matches the Id matches if not there is a difference in it. 您可以使用dev tools API在当前打开的页面(其中包含该元素)的DOM中搜索文本,如果该文本与Id匹配项匹配(如果没有)。 It could be any part eg. 它可以是任何部分。 6th character in Id being different :) ID中的第六个字符不同:)

Just to make sure that space between 1st part of id and 24 you mention is the problem you can either: 为了确保您提到的id的第一部分和24之间的空间是问题,您可以选择以下任一方法:

  1. look at webdriver's code and see internally what is used to access DOM elements 查看webdriver的代码,并在内部查看用于访问DOM元素的内容

  2. load jquery before testing this out: 在测试之前加载jQuery:

    var element = wait.Until(x => x.FindElements($('#id containing spaces')).SendKeys("foo"); var element = wait.Until(x => x.FindElements($('#id包含空格'))。SendKeys(“ foo”);

Basically, instead of using webdriver way to find element, you use jQuery to obtain the element reference. 基本上,您不是使用webdriver方法来查找元素,而是使用jQuery获取元素引用。 If that works it's the space problem due to bad design of the application having space in element's id 如果可行,那是空间问题,因为应用程序的不良设计在元素的id中有空格

Possibly that is why CSS selector route worked. 这可能就是CSS选择器路由起作用的原因。

If SendKeys() doesn't work you can try using JavaScript 如果SendKeys()不起作用,您可以尝试使用JavaScript

IWebElement webElement = element[2].FindElement(By.TagName("p"));
driver.ExecuteJavaScript("arguments[0].setAttribute('value', 'arguments[1]')", webElement, "foo");

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

相关问题 如何通过Selenium C#使用文本查找元素 - How to find an element using text through Selenium C# 如何通过Selenium和C#从元素获取文本 - How to Get the text from an element through Selenium and C# Selenium C# 使用 class 和文本查找元素 - Selenium C# Find Element with class and text 硒C#:如何再次查找元素 - Selenium C#: How to Find Element Again 如何通过Xpath,C#和Selenium从元素内的文本节点中检索文本 - How to retrieve the text from a text node within an element through Xpath, C# and Selenium 如何在文本区域内通过C#和Selenium发送具有style =“ display:none;”属性的文本 - How to send text within a textarea having the attribute style=“display: none;” through C# and Selenium Selenium Webdriver C#如何验证一个元素及其值为动态文本的值是否已经可见? - Selenium Webdriver C# How to verify if an element and it's value which is a dynamic text is already visible? 在无头模式 Selenium C# 中找不到文本元素 - Cant find Text Element in Headless Mode Selenium C# C# selenium 通过文本查找元素,然后单击复选框 - C# selenium find an element by text then click on checkbox 如何通过Selenium和C#根据给定的HTML单击元素 - How to click on the element as per the given HTML through Selenium and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM