[英]How to target multiple “li” with same classname
Hi everyone the function of my code is to take a users shoe size preference and then click on an element on the page based on their size. 大家好,我的代码的功能是考虑用户的鞋子尺码,然后根据他们的尺码在页面上单击一个元素。 I will do this by taking numerical input from the user and store it in a value, then searching for the element that matches the size they have entered.
我将通过从用户处获取数字输入并将其存储在一个值中,然后搜索与他们输入的大小匹配的元素来完成此操作。
Here is the html code from the website I am using: 这是我正在使用的网站上的html代码:
<div class="form product__form product__form--size product__form--last js-select-size-mapper">
<ul class = "product-size">
<li class = "js-size-value ">
8
</li>
<li class = "js-size-value ">
9
</li>
<li class = "js-size-value ">
9.5
</li>
<li class = "js-size-value ">
10
</li>
<li class = "js-size-value ">
10.5
</li>
</ul>
</div>
I always get some sort of error regarding how the element is not clickable. 关于元素不可点击的方式,我总是会遇到一些错误。 The weird part is sometimes the IWebElement declaration is verified (I mean there is no error in declaring it), sometimes there is an error when I try to click.
奇怪的是,有时会验证IWebElement声明(我的意思是声明它没有错误),有时当我尝试单击时出现错误。
If you need to see a better example of code, hop over to the website here . 如果需要查看更好的代码示例,请跳至此处的网站。
For example, when I run this code: 例如,当我运行以下代码时:
var element = driver.FindElement(By.XPath("//li[contains(@class,'js-size-value ')][contains(text(),size)]"));
element.Click();
*note, I have previously declared a variable called "size", and that's what I'm referring to when it says "(text(), size )]" *请注意,我之前已经声明了一个名为“ size”的变量,这就是我所说的变量“(text(), size )]”的含义。
I get the error: 我得到错误:
Additional information: Unexpected error.
附加信息:意外错误。 Element is not clickable at point (825.1500244140625, 45.69999694824219).
元素在点(825.1500244140625,45.69999694824219)上不可单击。 Other element would receive the click: div class="header__wrapper">
其他元素将获得点击:div class =“ header__wrapper”>
I don't know how to approach this, and need some light shed on it in general. 我不知道该如何处理,总体上需要一些启发。 I have searched through stackoverflow for answers, and haven't found any posts that pertain to exactlty what I am trying to accomplish, or are vague and hard to understand.
我已经在stackoverflow上搜索了答案,但是没有找到任何与我要完成的事情完全相关的帖子,或者含糊且难以理解。
Please take caution when answering that I am new to Selenium and C# in general, so if you could add a bit of detail in your answers and not assume I know what you are talking about, that would be great. 在回答我对Selenium和C#陌生时,请务必谨慎,因此,如果您可以在答案中添加一些细节而不假设我知道您在说什么,那将很棒。
You accidentally set size as text instead of as variable. 您不小心将大小设置为文本而不是变量。 This means selenium will search a list item with the text 'size', which doesn't exist, instead of for example size 9.5,
这意味着硒将搜索文本“大小”(不存在)(例如大小9.5)的列表项,
var element = driver.FindElement(By.XPath("//li[contains(@class,'js-size-value')][contains(text()," + size + ")]"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.