Search code examples
pythonseleniumfindelement

Python Selenium find_element_by functions can be called in chain continuously?


I'm trying to get an access to div.y element.

<div class="x">
  <div class="y"></div>
</div>

I know that I can directly get the div.y element using Selenium in Python.

But is it possible to call the functions like this?

browser.find_element_by_class_name("x").find_element_by_class_name("y")

Does the code above find div.y element only within the div.x element?


Solution

  • I'm not sure why you don't just try it, but yes, you can do so and it works as you probably expect. That is, it will find the first element with class y that is a descendant of the first x element on the page.