Selenium / Python - 通过 css 选择器进行选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18600391/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:12:56  来源:igfitidea点击:

Selenium / Python - Selecting via css selector

pythonseleniumwebdriver

提问by Dave

Issue:

问题:

Can not select from css selector specific element. Need to verify that the registered user can change their password successfully. I have tried the different attributes of the class to call it. The result is an exception error in the method when trying with the first two examples. The final try calls the first class instance and resets the password fields (fail).

无法从 css 选择器特定元素中进行选择。需要验证注册用户是否可以成功更改密码。我尝试了类的不同属性来调用它。结果是在尝试前两个示例时方法中出现异常错误。最后一次尝试调用第一个类实例并重置密码字段(失败)。

Tried:

尝试:

driver.find_element_by_css_selector("value.Update").click()
driver.find_element_by_css_selector("type.submit").click()
driver.find_element_by_css_selector("input.test_button4").click()

Objective:

客观的:

I need to select items that share the same class. As you can see below, the class is shared.

我需要选择共享同一类的项目。如下所示,该类是共享的。

form id="changepw_form" name="changepw" action="#" method="post">
<div class="field3">
<div class="field3">
<div class="field3">
<input class="test_button4" type="reset" value="Reset" style"font-size:21px"="">
<input class="test_button4" type="submit" value="Update" style"font-size:21px"="">

采纳答案by Dingredient

driver.find_element_by_css_selector(".test_button4[value='Update']").click()

EDIT: Because the selector needs a class, id, or tagname, but value.Updateby itself is none of these.

编辑:因为选择器需要一个class, id, or tagname,但value.Update它本身不是这些。

.test_button4provides a classname to match against, and from there, [value='Update']specifies which particular match(es) to select.

.test_button4提供要匹配的类名,并从那里[value='Update']指定要选择的特定匹配项。

回答by TehTris

test_button4 = driver.find_elements_by_class_name('test_button4') # notice its "find_elementS" with an S
submit_element = [x for x in test_button4 if x.get_attribute('value') == 'Update'] #this would work if you had unlimited things with class_name == 'test_button4', as long as only ONE of them is value="Update"
if len(submit_element): # using a non-empty list as truthiness
    print ("yay! found updated!")

This is something that i hardly ever see anyone document, explain, or use.

这是我几乎没有看到任何人记录、解释或使用的东西。

(ill use name for example because its simplest)

(例如使用名称,因为它最简单)

find_element_by_name()returns a single item, or gives an exception if it doesnt find it

find_element_by_name()返回单个项目,如果没有找到则给出异常

find_elements_by_name()returns a list of elements. if no elements found, the list is empty

find_elements_by_name()返回元素列表。如果没有找到元素,则列表为空

so if you do a find_elements_by_class_name()and it returns a list with X entries in it, all thats left at that point to narrow down what you want is either some old fashioned list comprehension ( like how i used in my answer ) or some indexing if you for some reason know which element you want.

因此,如果您执行 afind_elements_by_class_name()并返回一个包含 X 个条目的列表,那么此时剩下的只是缩小您想要的范围,或者是一些老式的列表理解(例如我在我的回答中使用的方式)或一些索引,如果您为某种原因知道你想要哪个元素。

also get_attribute()is seriously under-utilized as well. it parses the inside of the elements html by using what is before the =and returns what is after the =

get_attribute()被严重利用不足。它通过使用之前的内容来解析元素 html 的内部,=并返回之后的内容=