问题描述
我是硒的新手,我一直想找出一个问题:我在html块中输入了一个内容,我的任务是找到它并发送键.问题在于,不仅id是动态的,而且CSS选择器和Xpath
I'm kinda new to selenium, and I'm stuck at figuring out one problem: I have an input that's inside this html block and my task is locate it and send keys. The problem is that not only the id is dynamic, but also css selector and Xpath
一切看起来像这样
Xpath //*[@id="qf_25239c53-8f51-a9af-adff-fb4c61c369dd"]
CSSselector # qf_6fc767f5-f6d9-ce40-dc78-df0e43f6dc75
<label data-v-1a920554="" for="qf_b75b7021-5df4-2283-1c79-23bddcde3a3d" class="q-field row no-wrap items-start indent q-input q-field--outlined q-field--dense">
<div class="q-field__inner relative-position col self-stretch column justify-center">
<div tabindex="-1" class="q-field__control relative-position row no-wrap">
<div class="q-field__control-container col relative-position row no-wrap q-anchor--skip">
<input tabindex="0" placeholder="Заголовок" id="qf_b75b7021-5df4-2283-1c79-23bddcde3a3d" type="text" class="q-field__native q-placeholder"></div></div></div></label>
我这样做没有解决任何此类元素异常
I solved no such element exception by doing this
IWebElement MarkerNameInput = Setup.Driver.FindElement(By.XPath("(//*[@class = 'q-field__native q-placeholder'])[1]"));
但是它只解决了一个例外与另一个例外,现在我被困在:
But it had only fixed one exception with another and now I'm stuck at:
所以问题是找到输入的正确方法是什么,以便我可以在其中发送键?
So the question is what is the correct way to locate input, so I can send keys inside of it?
推荐答案
所需元素是动态元素,因此要调用 SendKeys()
,您必须诱导 WebDriverWait 与 ExpectedConditions as ElementToBeClickable方法(按),您可以使用以下任一解决方案:
The desired element is a dynamic element so to invoke SendKeys()
you have to induce WebDriverWait with ExpectedConditions as ElementToBeClickable Method (By) and you can use either of the following solutions:
-
CssSelector
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input.q-field__native.q-placeholder[id^='qf_'][placeholder='Заголовок']"))).SendKeys("Vladimir Krygin");
XPath
:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@class='q-field__native q-placeholder' and starts-with(@id, 'qf_')][@placeholder='Заголовок']"))).SendKeys("Vladimir Krygin");
这篇关于使用动态ID发出定位元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!