点击位于<li>元素中的元素。

huangapple go评论55阅读模式
英文:

Clicking Element which is in a <li> element

问题

嘿,我对Selenium还很新,我正在尝试单击下拉菜单中的一个元素,但错误消息是“指定了无效或非法的选择器”

这是我最后一次尝试:

IWebElement dropdown = driver.FindElement(By.XPath("//..."));
dropdown.Click();
IWebElement element = driver.FindElement(By.CssSelector("//..."));
jsexecutor.ExecuteScript("arguments[0].click()", element);

这是我最后一次尝试,但没有成功。谢谢您的提前帮助。
我在C#中使用Selenium。

英文:

Hey I am new to Selenium and I am trying to click an Element in a Dropdown Bar but the Error is that "an invalid or illegal selector was specified"

This was my last try:

IWebElement dropdown = driver.FindElement(By.XPath(&quot;//*[@id=\&quot;form-participation\&quot;]/div[4]/div[1]/span/span[1]/span/span[2]&quot;));
dropdown.Click();
IWebElement element = driver.FindElement(By.CssSelector(&quot;//*[@id=\&quot;select2-prize_id-result-ji85-2\&quot;]&quot;));
jsexecutor.ExecuteScript(&quot;arguments[0].click()&quot;, element);

That was my last try and it didn't work. Thanks for your help in Advance.
I use Selenium in C#.

答案1

得分: 0

select2-prize_id-result-ji85-2id 看起来是自动生成的,您可能应该寻找其他定位方式来找到所需的元素。

英文:

The select2-prize_id-result-ji85-2id looks auto-generated, you should probably look for alternative locators to get to the desired element.

答案2

得分: 0

//*[@id="select2-prize_id-result-ji85-2"] 不是一个 CSS 样式定位符,看起来更像是 XPath,而您尝试将其与 By.CssSelector 一起使用。

另外,最好在由 &quot; 符号限定的字符串内部使用 &#39; 而不是 &quot;。因此,我建议您的代码如下所示:

IWebElement dropdown = driver.FindElement(By.XPath("//[@id='form-participation']/div[4]/div[1]/span/span[1]/span/span[2]"));
dropdown.Click();
IWebElement element = driver.FindElement(By.XPath("//[@id='select2-prize_id-result-ji85-2']"));
jsexecutor.ExecuteScript("arguments[0].click()", element);

此外,由于第二个定位符完全基于元素 ID,您可以使用 By.Id 而不是 By.XPath,如下所示:

IWebElement dropdown = driver.FindElement(By.XPath("//[@id='form-participation']/div[4]/div[1]/span/span[1]/span/span[2]"));
dropdown.Click();
IWebElement element = driver.FindElement(By.Id("select2-prize_id-result-ji85-2"));
jsexecutor.ExecuteScript("arguments[0].click()", element);

另外,最后一个定位符中的 ji85-2 可能是动态值。我无法检查,因为您没有分享您正在使用的页面的链接。

更新
对于第一次点击,请尝试以下代码:

driver.FindElement(By.CssSelector(".select2-selection__arrow")).Click();

至于第二个元素,它是动态的,我们需要更多关于您的测试流程的信息,以了解如何选择该元素。

更新
好的,如果,例如,我们只想选择下拉列表中的第二个选项,可以使用以下代码:

driver.FindElement(By.CssSelector(".select2-selection__arrow")).Click();
driver.FindElement(By.XPath("(//li[contains(@class,'select2-results')])[2]")).Click();

索引 2 可以根据您想要选择的选项更改为 1 或 3 等。您可能需要在这里引入预期条件等待来使所有这些工作。或者至少,作为起步,使用命令之间的短延迟(暂停)。但在实际代码中,我们几乎从不使用硬编码的延迟。

英文:

//*[@id=\&quot;select2-prize_id-result-ji85-2\&quot;] is not a CSS style locator, it looks like XPath while you trying to use it with By.CssSelector
Also it would be better to use &#39; instead of &quot; inside a string limited by &quot; signs. So, I'd suggest your code to be as following:

IWebElement dropdown = driver.FindElement(By.XPath(&quot;//*[@id=&#39;form-participation&#39;]/div[4]/div[1]/span/span[1]/span/span[2]&quot;));
dropdown.Click();
IWebElement element = driver.FindElement(By.XPath(&quot;//*[@id=&#39;select2-prize_id-result-ji85-2&#39;]&quot;));
jsexecutor.ExecuteScript(&quot;arguments[0].click()&quot;, element);

Also, since the second locator is completely based on element ID you could use By.Id instead of By.XPath, as following:

IWebElement dropdown = driver.FindElement(By.XPath(&quot;//*[@id=&#39;form-participation&#39;]/div[4]/div[1]/span/span[1]/span/span[2]&quot;));
dropdown.Click();
IWebElement element = driver.FindElement(By.Id(&quot;select2-prize_id-result-ji85-2&quot;));
jsexecutor.ExecuteScript(&quot;arguments[0].click()&quot;, element);

Additionally, ji85-2 in the last locator may be dynamic value.
I can't check that since you did not share a link to page you working on.
UPD
For the first click please try this:

driver.FindElement(By.CssSelector(&quot;.select2-selection__arrow&quot;)).Click();

As about the second element it is dynamic, we need more information about your test flow to understand what element and how to select there.
UPD
OK, if, for example, we just want to select the second option in the doplist this can be used:

driver.FindElement(By.CssSelector(&quot;.select2-selection__arrow&quot;)).Click();  
driver.FindElement(By.XPath(&quot;(//li[contains(@class,&#39;select2-results&#39;)])[2]&quot;)).Click();

The index 2 can be changed to 1 or 3 etc accordingly to what option you want to select.
You probably will need to introduce expected condition wait here to make all this work. Or at least, just for beginning, to use sleeps (short delays) between command. But in real code we almost never use hardcoded sleeps.

huangapple
  • 本文由 发表于 2023年5月29日 23:45:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定