Selenium2+python Automation 73-Positioned Pit: Class attribute has a space "reprint"

Source: Internet
Author: User

Objective

Some class attributes have spaces in the middle, if copied directly to the location is the error invalidselectorexception:message:

The given selector U-label F-DN is either invalid or does not result in a webelement. The following error occurred:
Invalidselectorerror:compound class names not permitted

This error means that the positioning grammar is wrong.

First, positioning the class property with spaces

1. Take 126 Mailbox As example: http://mail.126.com/, Location account input Box

2. If you copy directly to use the class attribute to locate the error will be

Second, class attribute popular Science

The space in the middle of the 1.class attribute is not an empty string, which is a spacer symbol that represents an element with multiple class attribute names

(The class attribute is a special one, except that there are multiple attributes, other like Name,id are not multiple properties)

2. To learn the basics of HTML can refer to the Novice tutorial: http://www.runoob.com/html/html-attributes.html

Third, class positioning

1. Since it is known that the class attribute has a space of multiple attributes, then the positioning of the time to take one of the line (and to be unique), that is class= "j-inputtext dlemail",

It is OK to take j-inputtext and Dlemail, so this class property is unique on the page.

2. So the question is: How do you know if a property of this element is unique on the page?

Iv. determining the uniqueness of elements

1.F12 switch to the HTML interface, enter the keyword search in the search box, such as: J-inputtext, and then press ENTER to search, see the page has a few class attributes J-inputtext this attribute, you know is not unique.

V. What to do if the class attribute is not unique

1. If multiple properties of this class are not the only thing to do, the element is not unique and not afraid, you can use the plural positioning, all the same elements are positioned, the next mark to take the first few on the line.

Vi. positioning of CSS

1.css the element that locates the class attribute is preceded by a dot (.) and the space becomes a dot (.) to locate the

2. CSS can also take one of the properties of the class attribute (the only one on the page) to locate, the positioning method is flexible

Seven, reference code

# Coding:utf-8
From selenium import Webdriver
Driver = Webdriver. Firefox ()
Driver.get ("http://mail.126.com/")
Driver.implicitly_wait (20)

Driver.switch_to.frame ("X-urs-iframe")
# method One: Take a single class attribute
Driver.find_element_by_class_name ("Dlemail"). Send_keys ("Yoyo")
Driver.find_element_by_class_name ("Dlpwd"). Send_keys ("12333")

# method Two: Positioning a group to remove the target location (it is the worst way)
# driver.find_elements_by_class_name ("J-inputtext") [0].send_keys ("Yoyo")
# driver.find_elements_by_class_name ("J-inputtext") [1].send_keys ("12333")

# method Three: CSS positioning
# Driver.find_element_by_css_selector (". J-inputtext.dlemail"). Send_keys ("Yoyo")
# Driver.find_element_by_css_selector (". J-inputtext.dlpwd"). Send_keys ("123")

# method Four: It is also possible to take a single class attribute
# Driver.find_element_by_css_selector (". Dlemail"). Send_keys ("Yoyo")
# Driver.find_element_by_css_selector (". Dlpwd"). Send_keys ("123")

Selenium2+python Automation 73-Positioned Pit: Class attribute has a space "reprint"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.