简体   繁体   English

如何使用Selenium Webdriver选择具有多个CSS属性的元素

[英]How to select an element with multiple CSS attributes using Selenium Webdriver

Ok so I did my research for sure but couldn't find an answer to my problem. 好的,所以我确定要进行研究,但是找不到解决我问题的答案。 Using Selenium Webdriver and on the webpage there are a list of items below is a sample HTML 使用Selenium Webdriver,网页上的项目列表如下:HTML示例

    <div class="color-picker-slider" style="width: 180px;" active_state="black">
    <div class="store_color_picker black-selector" style="width:20px;" color_name="black"         product_id="132" base_color="black"></div>
    <div class="store_color_picker slate-selector" style="width:20px;" color_name="slate" product_id="133" base_color="slate"></div>
    <div class="store_color_picker violet-selector" style="width:20px;" color_name="violet" product_id="157" base_color="violet"></div>
    <div class="store_color_picker flexlime-selector" style="width:20px;" color_name="lime" product_id="155" base_color="lime"></div>
    <div class="store_color_picker pink-selector" style="width:20px;" color_name="pink" product_id="156" base_color="pink"></div>
    <div class="store_color_picker teal-selector" style="width:20px;" color_name="teal" product_id="158" base_color="teal"></div>
    <div class="store_color_picker tangerine-selector" style="width:20px;" color_name="tangerine" product_id="159" base_color="tangerine"></div>
    <div class="store_color_picker navy-selector" style="width:20px;" color_name="navy" product_id="160" base_color="navy"></div>
    <div class="store_color_picker red-selector" style="width:20px;" color_name="red" product_id="161" base_color="red"></div>
    </div>

So I created an enum that contains all of the products with it's respective index location number. 因此,我创建了一个枚举,其中包含所有产品及其相应的索引位置号。 And that works great but the only problem is if the indexes change, let's say adding a new item not adding to the last index, then I have to go and change each and every index, therefore not very efficient. 效果很好,但是唯一的问题是如果索引更改了,比方说添加一个新项而不添加到最后一个索引,那么我必须去更改每个索引,因此效率不高。 The only static attribute that I see is the product_id, which is unlikely to change. 我看到的唯一静态属性是product_id,该属性不太可能更改。 And if a new product_id is added I can just add it to the enum without having to change any indices and such. 如果添加了一个新的product_id,我可以将其添加到枚举中,而无需更改任何索引等。

My question then becomes how can I click on the element based on the product_id? 然后,我的问题变成如何根据product_id单击元素? I know how to get the attribute, but of course that returns a string but somehow I want to click on the item based on the product_id. 我知道如何获取属性,但是当然返回一个字符串,但是我想以某种方式单击基于product_id的项目。 Any help would be greatly appreciated! 任何帮助将不胜感激! I use CSS for finding elements by the way. 我用CSS来查找元素。 And this is not part of any dropdown list 这不是任何下拉列表的一部分

You can write a CSS selector based on its attributes: 您可以根据其属性编写CSS选择器:

.store_color_picker[product_id='133']

Basically, the format is: 基本上,格式为:

selector[attribute='attribute_value']

Thus, you could also do: 因此,您还可以这样做:

.store_color_picker[color_name='black']

if you wanted to select by the color name. 如果要选择颜色名称。

In Ruby Selenium (which is close to the Java version): 在Ruby Selenium(接近Java版本)中:

driver.find_element(:css, ".store_color_picker[product_id='133']")

Here's highly detailed documentation about css selectors , and you can find some more easily absorbed versions elsewhere as well. 这是有关css选择器非常详细的文档 ,您还可以在其他地方找到一些更容易吸收的版本。

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

相关问题 Selenium Webdriver - 如何在CSS菜单中选择元素 - Selenium Webdriver - How to select element in CSS Menu 如何使用Selenium WebDriver选择多个下拉菜单 - How to Select Multiple dropdowns using selenium webdriver 如何使用 Selenium WebDriver 读取 ::after 单选按钮元素的属性 - How to read ::after attributes of a radio button element using Selenium WebDriver 如何在 selenium webdriver java 中使用 css 选择器定位元素? - How to locate element using css selector in selenium webdriver java? 如何使用Selenium Webdriver滚动下拉列表并选择不可见/隐藏元素? - How to scroll the dropdown and select the invisible/hidden element using selenium webdriver? 如何使用Selenium WebDriver根据表元素选择表记录 - How to select a table record depending on table element using selenium webdriver 如何使用 Selenium Webdriver 在表中的不同位置选择多行 - How to select multiple rows at different place in a table using Selenium Webdriver 如何选择脚本Selenium Webdriver生成的元素 - how select element generated by script selenium webdriver 如何从列表中选择元素,单击它,返回列表,然后使用Selenium Webdriver选择下一个元素 - How to select element from the list, click on it, go back to the list and select next element using selenium webdriver 如何使用Selenium WebDriver在CSS中单击div - how to click the div in css using selenium webdriver
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM