Locating element by classname with double underscore

Viewed 860

I try to find an element by class name. Unfortunately the class name I have to select contains double underscores with raises a "no such element" error.

div-element to select:

<div class="result-list-entry__data">
...
</div>

selection:

last_named_class = result.find_element_by_class_name('result-list-entry__data')

Current result(error):

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".result-list-entry__data"} (Session info: chrome=77.0.3865.90)

Is it possible to escape the double underscores?

5 Answers

Perhaps you could use a contains query in your selector. Try something like this:

last_named_class = result.find_element_by_xpath("//div[contains(@class, 'result-list')]")

Hope this helps a bit.

First of all, check if there is any iframes. if the element is in iframe , you will have to switch to the frame, before doing anything with that element.

You can locate this element using partial string match on class attribute, something like this,

last_named_class = result.find_element_by_xpath("//*[contains(@class,'result-list-entry')]")

You can try to get the list of all elements with class = "result-list-entry__data" by using

If you have more than one class in the DOM iterate the elementResult array based on the position

 elementResult=[];
 elementResult = driver.find_elements_by_class_name("result-list-entry__data");

else

elementResult = driver.find_elements_by_class_name("result-list-entry__data");

You can use the approach suggested before with two contains:

last_named_class = result.find_element_by_xpath("//div[contains(@class, 'result-list') and contains(@class, 'data')]"

Try below things,

  1. Apply required wait (implicit or explicit wait)
    driver.implicitly_wait(15)
wait = WebDriverWait(driver, 10)
wait.until(ec.visibility_of_element_located((By.XPATH, "//*[contains(@class,'result-list-entry')]")))
  1. Check whether element is a child of iframe element. if so, switch to iframe and try to click

  2. Use xpath with contains if the element property value changing frequently

    last_named_class = result.find_element_by_xpath("//*[contains(@class,'result-list-entry')]")
Related