1

I would like to select elements by the class name, but only with a specific name(first-level-li), if there are other classes with the name that I am trying to select but followed for other names

(<li class="first-level-li no-second-level shop-link">
<li class="first-level-li  no-second-level  ">)

, I need to ignore them, how can I do this? Example:

firstLevelMenu = self.driver.find_elements_by_class_name ( "first-level-li" )

website

<li class="first-level-li no-second-level shop-link">
<li class="first-level-li  no-second-level  ">
<li class="first-level-li  ">
6
  • you can select with xpath Commented Oct 24, 2017 at 10:31
  • unfortunately, using by xpath (find_elements_by_xpath) is not returning any elements, and there are 9 elements in the page. Do you know the reason? Commented Oct 24, 2017 at 11:31
  • print len(firstLevelMenu) Commented Oct 24, 2017 at 11:33
  • What is the url? Commented Oct 24, 2017 at 12:04
  • @SDB in the example html you gave, the class name has 2 spaces after it. So your xpath to match that would actually be with the 2 spaces ("//li[@class='first-level-li ']"). It has to match exactly Commented Oct 24, 2017 at 12:05

3 Answers 3

1
firstlevelMenu = self.driver.find_elements_by_xpath("//li[@class='first-level-li  ']")

Docs are here http://selenium-python.readthedocs.io/locating-elements.html

Sign up to request clarification or add additional context in comments.

1 Comment

unfortunately, using by xpath (find_elements_by_xpath) is not returning any elements, and there are 9 elements in the page. Do you know the reason?
0

You can use xpath to do the same:

self.driver.find_elements_by_xpath("//li[@class='first-level-li']")

This finds the element that has the exact class name.

Comments

0

You can use xpath as mentioned above with following function too which will directly return object of specific element, instead of list.

self.driver.find_element_by_xpath("//li[@class='first-level-li ']")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.